{"id":7324,"date":"2020-05-09T14:58:38","date_gmt":"2020-05-09T09:28:38","guid":{"rendered":"https:\/\/www.csestack.org\/?p=7324"},"modified":"2020-05-09T14:58:40","modified_gmt":"2020-05-09T09:28:40","slug":"convert-nested-dictionary-into-flattened-python","status":"publish","type":"post","link":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/","title":{"rendered":"[Solved] Convert Nested Dictionary into Flattened in Python"},"content":{"rendered":"\n<p><strong>Problem Statement:<\/strong><\/p>\n\n\n\n<p>You have given a nested dictionary (dictionary inside another dictionary). Write a program to convert the nested dictionary into a nested dictionary.<\/p>\n\n\n\n<p>Let&#8217;s take an below example.<\/p>\n\n\n\n<p><strong>Input Dictionary:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{  'a' : 1,\n   'b' : { \n           'c': 3,\n            'd': 4\n    }\n}<\/pre>\n\n\n\n<p><strong>Output (Flatten) Dictionary:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{  'a' : 1,\n   'b' : 2,\n   'c': 3,\n   'd': 4\n}<\/pre>\n\n\n\n<p>Here the value of &#8216;b&#8217; in output dictionary is two as there are two elements having key &#8216;c&#8217; and &#8216;d&#8217; in the given nested dictionary. <\/p>\n\n\n\n<p>Elements in the nested dictionary will be added as it is in the output dictionary.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Algorithm:<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Take an empty dictionary for output (says &#8216;out_dict&#8217;)<\/li><li>Traverse through all the elements in the dictionary.<ul><li>If the data type of the value is &#8216;dict&#8217;, add a new element in the out_dict in the dictionary with the same &#8216;key&#8217; and length if the nested dictionary as value. Append the nested dictionary as it is to the out_dict.<\/li><li>Else, element as it is to the out_dict.<\/li><\/ul><\/li><li>Print the &#8216;out_dict&#8217;.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Program<\/strong> to Convert Nested Dictionary into Flattened in Python:<\/h4>\n\n\n\n<p>We are using items() method to iterate over all the elements in the dictionary and update() method for adding one dictionary to another dictionary.<\/p>\n\n\n\n<p>Check <a href=\"https:\/\/www.csestack.org\/python-dictionary\/\">Python dictionary tutorial<\/a> for more.<\/p>\n\n\n\n<p><strong>Program:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">def flat_dict(sample_dict):\n  out_dict = {}\n\n  for key, val in sample_dict.items():\n    if type(val) == dict:\n      out_dict[key] = len(sample_dict.keys())\n      out_dict.update(val)\n    else:\n      out_dict[key] = val\n\n  return out_dict\n\n\nif __name__ == \"__main__\":\n  sample_dict = {'a' : 1, 'b' : {'c': 3, 'd': 4}}\n  out_dict = flat_dict(sample_dict)\n  print(out_dict)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{'a': 1, 'b': 2, 'c': 3, 'd': 4}<\/pre>\n\n\n\n<p>This program was asked in <a href=\"https:\/\/www.csestack.org\/tag\/juniper\/\">Juniper interview for Python developer<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Complexity:<\/strong><\/h4>\n\n\n\n<p>Every element in the dictionary traversed only once. The <strong>time complexity is O(n)<\/strong> where n is the size of the dictionary.<\/p>\n\n\n\n<p>We are creating a new dictionary to save the flatten dictionary. The <strong>space complexity is O(n)<\/strong>, where n is the size of the dictionary.<\/p>\n\n\n\n<p>Sometimes, in the interview coding round, the interviewer may ask you to solve this problem without creating any extras space. In that case, you can modify the given dictionary instead of creating a new one. The space complexity will become O(1).<\/p>\n\n\n\n<p>This is all about a program to convert nested dictionary into flattened in Python. Also, check other <a href=\"https:\/\/www.csestack.org\/interview-coding-questions-programming-language\/\">coding interview questions<\/a> for practicing and improving your programming skills.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to convert nested dictionary into flattened in Python? This questions was asked in Juniper coding interview. Algorithm and program explained.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,73],"tags":[72],"class_list":["post-7324","post","type-post","status-publish","format-standard","hentry","category-code","category-python","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Convert Nested Dictionary into Flattened in Python<\/title>\n<meta name=\"description\" content=\"How to convert nested dictionary into flattened in Python? This questions was asked in Juniper coding interview. Algorithm and program explained.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Convert Nested Dictionary into Flattened in Python\" \/>\n<meta property=\"og:description\" content=\"How to convert nested dictionary into flattened in Python? This questions was asked in Juniper coding interview. Algorithm and program explained.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/\" \/>\n<meta property=\"og:site_name\" content=\"CSEstack\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-09T09:28:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-09T09:28:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aniruddha Chaudhari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:site\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aniruddha Chaudhari\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/\"},\"author\":{\"name\":\"Aniruddha Chaudhari\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"headline\":\"[Solved] Convert Nested Dictionary into Flattened in Python\",\"datePublished\":\"2020-05-09T09:28:38+00:00\",\"dateModified\":\"2020-05-09T09:28:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/\"},\"wordCount\":324,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"keywords\":[\"Python\"],\"articleSection\":[\"Code\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/\",\"name\":\"[Solved] Convert Nested Dictionary into Flattened in Python\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\"},\"datePublished\":\"2020-05-09T09:28:38+00:00\",\"dateModified\":\"2020-05-09T09:28:40+00:00\",\"description\":\"How to convert nested dictionary into flattened in Python? This questions was asked in Juniper coding interview. Algorithm and program explained.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/convert-nested-dictionary-into-flattened-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.csestack.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Convert Nested Dictionary into Flattened in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/\",\"name\":\"CSEstack\",\"description\":\"Computer Science &amp; Programming Portal\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.csestack.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\",\"name\":\"Aniruddha Chaudhari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"width\":634,\"height\":634,\"caption\":\"Aniruddha Chaudhari\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\"},\"description\":\"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.\",\"sameAs\":[\"https:\\\/\\\/www.csestack.org\",\"https:\\\/\\\/www.facebook.com\\\/aniruddha.ca\",\"pythonwithani\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aniruddha28\\\/\",\"https:\\\/\\\/x.com\\\/ani_chaudhari\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCw0a__B0eJsvCujkSIfLTAA\"],\"url\":\"https:\\\/\\\/www.csestack.org\\\/author\\\/anicse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Convert Nested Dictionary into Flattened in Python","description":"How to convert nested dictionary into flattened in Python? This questions was asked in Juniper coding interview. Algorithm and program explained.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Convert Nested Dictionary into Flattened in Python","og_description":"How to convert nested dictionary into flattened in Python? This questions was asked in Juniper coding interview. Algorithm and program explained.","og_url":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/","og_site_name":"CSEstack","article_publisher":"https:\/\/www.facebook.com\/aniruddha.ca","article_author":"https:\/\/www.facebook.com\/aniruddha.ca","article_published_time":"2020-05-09T09:28:38+00:00","article_modified_time":"2020-05-09T09:28:40+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg","type":"image\/jpeg"}],"author":"Aniruddha Chaudhari","twitter_card":"summary_large_image","twitter_creator":"@ani_chaudhari","twitter_site":"@ani_chaudhari","twitter_misc":{"Written by":"Aniruddha Chaudhari","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/#article","isPartOf":{"@id":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/"},"author":{"name":"Aniruddha Chaudhari","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"headline":"[Solved] Convert Nested Dictionary into Flattened in Python","datePublished":"2020-05-09T09:28:38+00:00","dateModified":"2020-05-09T09:28:40+00:00","mainEntityOfPage":{"@id":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/"},"wordCount":324,"commentCount":0,"publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"keywords":["Python"],"articleSection":["Code","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/","url":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/","name":"[Solved] Convert Nested Dictionary into Flattened in Python","isPartOf":{"@id":"https:\/\/www.csestack.org\/#website"},"datePublished":"2020-05-09T09:28:38+00:00","dateModified":"2020-05-09T09:28:40+00:00","description":"How to convert nested dictionary into flattened in Python? This questions was asked in Juniper coding interview. Algorithm and program explained.","breadcrumb":{"@id":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.csestack.org\/convert-nested-dictionary-into-flattened-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.csestack.org\/"},{"@type":"ListItem","position":2,"name":"[Solved] Convert Nested Dictionary into Flattened in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.csestack.org\/#website","url":"https:\/\/www.csestack.org\/","name":"CSEstack","description":"Computer Science &amp; Programming Portal","publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.csestack.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218","name":"Aniruddha Chaudhari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","width":634,"height":634,"caption":"Aniruddha Chaudhari"},"logo":{"@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg"},"description":"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.","sameAs":["https:\/\/www.csestack.org","https:\/\/www.facebook.com\/aniruddha.ca","pythonwithani","https:\/\/www.linkedin.com\/in\/aniruddha28\/","https:\/\/x.com\/ani_chaudhari","https:\/\/www.youtube.com\/channel\/UCw0a__B0eJsvCujkSIfLTAA"],"url":"https:\/\/www.csestack.org\/author\/anicse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/7324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/comments?post=7324"}],"version-history":[{"count":2,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/7324\/revisions"}],"predecessor-version":[{"id":7326,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/7324\/revisions\/7326"}],"wp:attachment":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media?parent=7324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/categories?post=7324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/tags?post=7324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}