{"id":3595,"date":"2020-07-15T16:47:11","date_gmt":"2020-07-15T11:17:11","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3595"},"modified":"2021-06-14T15:11:42","modified_gmt":"2021-06-14T09:41:42","slug":"nested-dictionary-python","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/","title":{"rendered":"Nested Dictionary in Python: Storing Data Made Easy"},"content":{"rendered":"\n<p>Whenever we talk about storing data in the form of <strong>key-value pair<\/strong> in python, the data structure that comes first into mind is a <strong>python dictionary<\/strong>. If you come from a JAVA background, you must be familiar with <strong>hash maps<\/strong> and hash tables. If we want to <strong>store information in a structured way<\/strong>, creating a nested dictionary in python can be one of the better options.<br>Nested Dictionary is nothing but a dictionary inside the Dictionary. In nested dictionaries, some keys have another dictionary as its values. Let us look at an example to understand it better.<br><strong>{&#8220;Sameer&#8221;:{&#8220;English&#8221;:91},&#8221;Tarun&#8221;:{&#8220;English&#8221;:76},&#8221;Archit&#8221;:{&#8220;English&#8221;:92}}<\/strong><br>We can see that by using a nested dictionary, we can store information for different students and their marks. And the best thing it is so easy to retrieve. It wouldn\u2019t have been possible with lists or any other data type.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#How_to_Create_Nested_Dictionaries_in_Python\" >How to Create Nested Dictionaries in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Standard_Dictionary\" >Standard Dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Nested_Dictionary\" >Nested Dictionary<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Adding_New_Elements_in_a_Nested_Dictionary_in_Python\" >Adding New Elements in a Nested Dictionary in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Method-1\" >Method-1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Method-2\" >Method-2<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Method_3\" >Method 3-<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Changing_The_Values_in_Nested_Dictionaries_in_Python\" >Changing The Values in Nested Dictionaries in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Deleting_an_Entry_in_a_Nested_Dictionary_in_Python\" >Deleting an Entry in a Nested Dictionary in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Retrieving_Values_from_Nested_Dictionaries_in_Python\" >Retrieving Values from Nested Dictionaries in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Must_Read\" >Must Read<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-how-to-create-nested-dictionaries-in-python\"><span class=\"ez-toc-section\" id=\"How_to_Create_Nested_Dictionaries_in_Python\"><\/span>How to Create Nested Dictionaries in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Creating a nested dictionary in python is not rocket science. For starters, let us first learn how to create a standard dictionary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-standard-dictionary\"><span class=\"ez-toc-section\" id=\"Standard_Dictionary\"><\/span>Standard Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# To create a normal dictionary we can use dict().\ndict1=dict()\n# key='a', value=20\ndict1&#x5B;'a']=20\n# key='b', value=21\ndict1&#x5B;'b']=21\nprint(&quot;dict1:&quot;,dict1)\n# Using {}\ndict2={}\ndict2&#x5B;'c']=50\ndict2&#x5B;'d']=51\nprint(&quot;dict2:&quot;,dict2)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n\ndict1: {'a': 20, 'b': 21}\ndict2: {'c': 50, 'd': 51}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nested-dictionary\"><span class=\"ez-toc-section\" id=\"Nested_Dictionary\"><\/span>Nested Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-method-1\">Method-1<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# making an empty dictionary\nstudent_info={}\n# Adding value to key='ashwini' as another dictonary\nstudent_info&#x5B;'ashwini'] ={'maths':92,'english':90,'science':80}\nstudent_info&#x5B;'archit'] ={'maths':93,'english':80,'science':90}\nstudent_info&#x5B;'tarun'] ={'maths':82,'english':86,'science':95}\nprint(student_info)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n{'ashwini': {'maths': 92, 'english': 90, 'science': 80}, 'archit': {'maths': 93, 'english': 80, 'science': 90}, 'tarun': {'maths': 82, 'english': 86, 'science': 95}}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-method-2-using-zip\">Method 2 Using zip()<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Storing the id's into a list\nEmployee_id=&#x5B;1,2,3]\n# Storing the info in another dictionary\nEmployee_info = &#x5B; { 'Name': 'Ashish' , 'Age' : 33 , &quot;Designation&quot; : &quot;Web Developer&quot;} , { 'Name' : 'Shubham' , 'Age' : 23 , &quot;Designation&quot; : &quot;IOS APP Developer&quot; } , { 'Name' : 'Vandana' , 'Age' : 25, &quot;Designation&quot; : &quot;Data Scientist&quot; }]\n# Mapping the id's with employee information\nEmployee=dict(zip(Employee_id,Employee_info))\nprint('Employee Information',Employee)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n\nEmployee Information {1: {'Name': 'Ashish', 'Age': 33, 'Designation': 'Web Developer'}, 2: {'Name': 'Shubham', 'Age': 23, 'Designation': 'IOS APP Developer'}, 3: {'Name': 'Vandana', 'Age': 25, 'Designation': 'Data Scientist'}}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-adding-new-elements-in-a-nested-dictionary-in-python\"><span class=\"ez-toc-section\" id=\"Adding_New_Elements_in_a_Nested_Dictionary_in_Python\"><\/span>Adding New Elements in a Nested Dictionary in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Suppose we want to add new elements to the pre-created dictionary. Let\u2019s see how we do this.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-method-1-1\"><span class=\"ez-toc-section\" id=\"Method-1\"><\/span>Method-1 <span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEmployee_id=&#x5B;1,2,3,4,5]\nEmployee_info=&#x5B;{'Name':'Ashish','Age':33,&quot;Designation&quot;:&quot;Web Developer&quot;},{'Name':'Shubham','Age':23,&quot;Designation&quot;:&quot;IOS APP Developer&quot;},{'Name':'Vandana','Age':25,&quot;Designation&quot;:&quot;Data Scientist&quot;}]\nEmployee=dict(zip(Employee_id,Employee_info))\n#Here we are adding new info of Employee with 'id':4\nEmployee&#x5B;4]={'Name':'Ashutosh','Age':40,&quot;Designation&quot;:&quot;Cyber Security Expert&quot;}\nprint(Employee&#x5B;4])\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n{'Name': 'Ashutosh', 'Age': 40, 'Designation': 'Cyber Security Expert'}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-method-2\"><span class=\"ez-toc-section\" id=\"Method-2\"><\/span><br>Method-2<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEmployee_id=&#x5B;1,2,3]\nEmployee_info=&#x5B;{'Name':'Ashish','Age':33,&quot;Designation&quot;:&quot;Web Developer&quot;},{'Name':'Shubham','Age':23,&quot;Designation&quot;:&quot;IOS APP Developer&quot;},{'Name':'Vandana','Age':25,&quot;Designation&quot;:&quot;Data Scientist&quot;}]\nEmployee=dict(zip(Employee_id,Employee_info))\n# Create a new dictionary for Employee with id-4\nEmployee&#x5B;4]={}\n# Adding NAME of Employee\nEmployee&#x5B;4]&#x5B;'Name']='Manya'\n# Adding AGE of Employee\nEmployee&#x5B;4]&#x5B;'Age']=29\n# Adding DESIGNATION of Employee\nEmployee&#x5B;4]&#x5B;'Designation']='HR'\nprint(Employee)\n<\/pre><\/div>\n\n\n<p><strong>Output-<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{1: {'Name': 'Ashish', 'Age': 33, 'Designation': 'Web Developer'}, 2: {'Name': 'Shubham', 'Age': 23, 'Designation': 'IOS APP Developer'}, 3: {'Name': 'Vandana', 'Age': 25, 'Designation': 'Data Scientist'}, 4: {'Name': 'Manya', 'Age': 29, 'Designation': 'HR'}}<\/pre>\n\n\n\n<p><br>You can observe that the new entries have been added at the end successfully.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-method-3\"><span class=\"ez-toc-section\" id=\"Method_3\"><\/span>Method 3-<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Suppose if we want to add multiple entries to a dictionary. We can use For loop for that.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEmployee_id=&#x5B;1,2,3]\nEmployee_info=&#x5B;{'Name':'Ashish','Age':33,&quot;Designation&quot;:&quot;Web Developer&quot;},{'Name':'Shubham','Age':23,&quot;Designation&quot;:&quot;IOS APP Developer&quot;},{'Name':'Vandana','Age':25,&quot;Designation&quot;:&quot;Data Scientist&quot;}]\nEmployee=dict(zip(Employee_id,Employee_info))\n# Checking the length of the dictionary before addition\ntotal_employees=len(Employee)\nids=int(input(&quot;How many Entries you want to add:&quot;))\n# Adding new Employees \nfor i in range(total_employees+1,total_employees+ids+1):\n    Employee&#x5B;i]={}\n    # Adding NAME of Employee\n    Employee&#x5B;i]&#x5B;'Name']=input(&quot;Enter the name of new Employee:&quot;)\n    # Adding AGE of Employee\n    Employee&#x5B;i]&#x5B;'Age']=int(input(&quot;Enter the age of new Employee:&quot;))\n    # Adding DESIGNATION of Employee\n    Employee&#x5B;i]&#x5B;'Designation']=input(&quot;Enter the designation of new Employee:&quot;)\nprint(Employee)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"654\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-13-1024x654.png\" alt=\"nested dictionary python\" class=\"wp-image-3596\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-13-1024x654.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-13-300x192.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-13-768x491.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-13-1536x981.png 1536w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-13-20x13.png 20w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-13.png 1584w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">Output-\n\nHow many Entries you want to add:4\nEnter the name of new Employee:kunal\nEnter the age of new Employee:29\nEnter the designation of new Employee:Data Analyst\nEnter the name of new Employee:Kushal\nEnter the age of new Employee:49\nEnter the designation of new Employee:System Architect\nEnter the name of new Employee:Preeti\nEnter the age of new Employee:36\nEnter the designation of new Employee:Researcher\nEnter the name of new Employee:Aarti\nEnter the age of new Employee:22\nEnter the designation of new Employee: Content Writer\n\n{1: {'Name': 'Ashish', 'Age': 33, 'Designation': 'Web Developer'}, 2: {'Name': 'Shubham', 'Age': 23, 'Designation': 'IOS APP Developer'}, 3: {'Name': 'Vandana', 'Age': 25, 'Designation': 'Data Scientist'}, 4: {'Name': 'kunal', 'Age': 29, 'Designation': 'Data Analyst'}, 5: {'Name': 'Kushal', 'Age': 49, 'Designation': 'System Architect'}, 6: {'Name': 'Preeti', 'Age': 36, 'Designation': 'Researcher'}, 7: {'Name': 'Aarti', 'Age': 22, 'Designation': 'Content Writer'}} <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-changing-the-values-in-nested-dictionaries-in-python\"><span class=\"ez-toc-section\" id=\"Changing_The_Values_in_Nested_Dictionaries_in_Python\"><\/span>Changing The Values in Nested Dictionaries in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this section, we will learn how to update values in a nested dictionary.<br>For example, suppose if, in a company, an employee named Ashish, who was working as a web developer, has been promoted to Senior Web Developer. Let\u2019s see how we can update this value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;pre class=&quot;wp-block-syntaxhighlighter-code&quot;&gt;Employee_id=&#x5B;1,2,3,4,5]\nEmployee_info=&#x5B;{'Name':'Ashish','Age':33,&quot;Designation&quot;:&quot;Web Developer&quot;},{'Name':'Shubham','Age':23,&quot;Designation&quot;:&quot;IOS APP Developer&quot;},{'Name':'Vandana','Age':25,&quot;Designation&quot;:&quot;Data Scientist&quot;}]\nEmployee=dict(zip(Employee_id,Employee_info))\nprint(&quot;Employee information before updation&quot;,Employee,&quot;\\n&quot;)\n# &lt;a href=&quot;http:\/\/www.pythonpool.com\/python-iterate-through-list\/&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;Iterating&lt;\/a&gt; through the values in employee dictionary\nfor i in Employee.values():\n    # Look for an employee with name 'Ashish'\n    if i&#x5B;'Name']=='Ashish':\n        # Change the designation to 'Sr. Web Developer'\n        i&#x5B;'Designation']='Sr. Web Developer'\nprint(&quot;Employee information after updation&quot;,Employee)&lt;\/pre&gt;\n<\/pre><\/div>\n\n\n<p>Output-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>Employee information before updation<\/em><\/strong> {1: {'Name': 'Ashish', 'Age': 33, 'Designation': 'Web Developer'}, 2: {'Name': 'Shubham', 'Age': 23, 'Designation': 'IOS APP Developer'}, 3: {'Name': 'Vandana', 'Age': 25, 'Designation': 'Data Scientist'}}\n\n<strong><em>Employee information after updation <\/em><\/strong>{1: {'Name': 'Ashish', 'Age': 33, 'Designation': 'Sr. Web Developer'}, 2: {'Name': 'Shubham', 'Age': 23, 'Designation': 'IOS APP Developer'}, 3: {'Name': 'Vandana', 'Age': 25, 'Designation': 'Data Scientist'}}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-deleting-an-entry-in-a-nested-dictionary-in-python\"><span class=\"ez-toc-section\" id=\"Deleting_an_Entry_in_a_Nested_Dictionary_in_Python\"><\/span>Deleting an Entry in a Nested Dictionary in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Suppose if an employee has left the company. Let us have a look at how we will drop that entry.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEmployee_id=&#x5B;1,2,3,4,5]\nEmployee_info=&#x5B;{'Name':'Ashish','Age':33,&quot;Designation&quot;:&quot;Web Developer&quot;},{'Name':'Shubham','Age':23,&quot;Designation&quot;:&quot;IOS APP Developer&quot;},{'Name':'Vandana','Age':25,&quot;Designation&quot;:&quot;Data Scientist&quot;}]\nEmployee=dict(zip(Employee_id,Employee_info))\nprint(&quot;Employee before Deletion : &quot;,Employee,&quot;\\n&quot;)\n# Delete the employee record with id=2\nEmployee.pop(2)\nprint(&quot;Employee after Deletion : &quot;,Employee)\n<\/pre><\/div>\n\n\n<p>Output-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Employee before Deletion : {1: {'Name': 'Ashish', 'Age': 33, 'Designation': 'Web Developer'}, 2: {'Name': 'Shubham', 'Age': 23, 'Designation': 'IOS APP Developer'}, 3: {'Name': 'Vandana', 'Age': 25, 'Designation': 'Data Scientist'}}\n\nEmployee after Deletion: {1: {'Name': 'Ashish', 'Age': 33, 'Designation': 'Web Developer'}, 3: {'Name': 'Vandana', 'Age': 25, 'Designation': 'Data Scientist'}}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-retrieving-values-from-nested-dictionaries-in-python\"><span class=\"ez-toc-section\" id=\"Retrieving_Values_from_Nested_Dictionaries_in_Python\"><\/span>Retrieving Values from Nested Dictionaries in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us iterate through this nested Dictionary using for loop.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEmployee_id=&#x5B;1,2,3]\nEmployee_info=&#x5B;{'Name':'Ashish','Age':33,&quot;Designation&quot;:&quot;Web Developer&quot;},{'Name':'Shubham','Age':23,&quot;Designation&quot;:&quot;IOS APP Developer&quot;},{'Name':'Vandana','Age':25,&quot;Designation&quot;:&quot;Data Scientist&quot;}]\nEmployee=dict(zip(Employee_id,Employee_info))\n# Iterating through employee keys\nfor i in Employee.keys():\n    print(&quot;Employee Id:&quot;,i)\n# Iterating through the values of Employee keys\n    for j in Employee&#x5B;i]:\n        print(j,&quot;:&quot;,Employee&#x5B;i]&#x5B;j])\n    print(&quot;\\n&quot;)\n<\/pre><\/div>\n\n\n<p>Output-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Employee Id: 1\nName : Ashish\nAge : 33\nDesignation: Web Developer\n\nEmployee Id: 2\nName: Shubham\nAge: 23\nDesignation: IOS APP Developer\n\nEmployee Id: 3\nName : Vandana\nAge : 25\nDesignation : Data Scientist<\/pre>\n\n\n\n<p>Now, if we want to directly access the age of Employee with id=2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEmployee_id=&#x5B;1,2,3]\nEmployee_info=&#x5B;{'Name':'Ashish','Age':33,&quot;Designation&quot;:&quot;Web Developer&quot;},{'Name':'Shubham','Age':23,&quot;Designation&quot;:&quot;IOS APP Developer&quot;},{'Name':'Vandana','Age':25,&quot;Designation&quot;:&quot;Data Scientist&quot;}]\nEmployee=dict(zip(Employee_id,Employee_info))\n# Age of employee with id 2\nprint(Employee&#x5B;2]&#x5B;'Age'])\n<\/pre><\/div>\n\n\n<p>Output-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">23<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-must-read\"><span class=\"ez-toc-section\" id=\"Must_Read\"><\/span><span class=\"has-inline-color has-luminous-vivid-orange-color\">Must Read<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.pythonpool.com\/python-lowercase\/\">How to Convert String to Lowercase in<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/square-root-in-python\/\">How to Calculate Square Root<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-user-input\/\">User Input | Input () Function | Keyboard Input<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-book\/\">Best Book to Learn Python<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Nested dictionaries in python have proved to be very useful when we have to store data in a structured way. We have discussed how to store data in a nested dictionary, how to retrieve the values stored in it, how to delete, and update values from it in many different ways. <\/p>\n\n\n\n<p>Try to run the programs on your side and let us know if you have any queries.<\/p>\n\n\n\n<p><strong><em>Happy Coding!<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whenever we talk about storing data in the form of key-value pair in python, the data structure that comes first into mind is a python &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Nested Dictionary in Python: Storing Data Made Easy\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#more-3595\" aria-label=\"More on Nested Dictionary in Python: Storing Data Made Easy\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":3603,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[1536,1531,1528,1538,1540,1529,1526,1530,1535,1539,1525,1533,1527,1532,1534,1537],"class_list":["post-3595","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-access-nested-dictionary-python","tag-accessing-a-nested-dictionary-python","tag-convert-a-nested-list-into-a-dictionary-lname-python","tag-convert-nested-dictionary-to-list-python","tag-create-nested-dictionary-python","tag-how-to-access-nested-dictionary-in-python","tag-how-to-create-a-nested-dictionary-in-python","tag-initialize-nested-dictionary-python","tag-iterate-over-a-nested-dictionary-python","tag-nested-dictionary-python","tag-print-nested-dictionary-python","tag-python-create-nested-dictionary-from-csv","tag-python-get-all-values-from-nested-dictionary","tag-python-nested-dictionary","tag-python-nested-dictionary-access","tag-python-update-nested-dictionary","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Nested Dictionary in Python: Storing Data Made Easy - Python Pool<\/title>\n<meta name=\"description\" content=\"To store information in a structured way we can use nested dictionary in python. It is also known as a dictionary inside a dictionary.\" \/>\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.pythonpool.com\/nested-dictionary-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nested Dictionary in Python: Storing Data Made Easy\" \/>\n<meta property=\"og:description\" content=\"Whenever we talk about storing data in the form of key-value pair in python, the data structure that comes first into mind is a python dictionary. If you\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-15T11:17:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-14T09:41:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1002\" \/>\n\t<meta property=\"og:image:height\" content=\"606\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwini Mandani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/\"},\"author\":{\"name\":\"Ashwini Mandani\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056\"},\"headline\":\"Nested Dictionary in Python: Storing Data Made Easy\",\"datePublished\":\"2020-07-15T11:17:11+00:00\",\"dateModified\":\"2021-06-14T09:41:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/\"},\"wordCount\":469,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg\",\"keywords\":[\"access nested dictionary python\",\"accessing a nested dictionary python\",\"convert a nested list into a dictionary lname python\",\"convert nested dictionary to list python\",\"create nested dictionary python\",\"how to access nested dictionary in python\",\"how to create a nested dictionary in python\",\"initialize nested dictionary python\",\"iterate over a nested dictionary python\",\"nested dictionary python\",\"print nested dictionary python\",\"python create nested dictionary from csv\",\"python get all values from nested dictionary\",\"python nested dictionary\",\"python nested dictionary access\",\"python update nested dictionary\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/\",\"url\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/\",\"name\":\"Nested Dictionary in Python: Storing Data Made Easy - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg\",\"datePublished\":\"2020-07-15T11:17:11+00:00\",\"dateModified\":\"2021-06-14T09:41:42+00:00\",\"description\":\"To store information in a structured way we can use nested dictionary in python. It is also known as a dictionary inside a dictionary.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg\",\"width\":1002,\"height\":606,\"caption\":\"nested dictionary python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Nested Dictionary in Python: Storing Data Made Easy\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056\",\"name\":\"Ashwini Mandani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"caption\":\"Ashwini Mandani\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Nested Dictionary in Python: Storing Data Made Easy - Python Pool","description":"To store information in a structured way we can use nested dictionary in python. It is also known as a dictionary inside a dictionary.","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.pythonpool.com\/nested-dictionary-python\/","og_locale":"en_US","og_type":"article","og_title":"Nested Dictionary in Python: Storing Data Made Easy","og_description":"Whenever we talk about storing data in the form of key-value pair in python, the data structure that comes first into mind is a python dictionary. If you","og_url":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/","og_site_name":"Python Pool","article_published_time":"2020-07-15T11:17:11+00:00","article_modified_time":"2021-06-14T09:41:42+00:00","og_image":[{"width":1002,"height":606,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg","type":"image\/jpeg"}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Ashwini Mandani","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/"},"author":{"name":"Ashwini Mandani","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056"},"headline":"Nested Dictionary in Python: Storing Data Made Easy","datePublished":"2020-07-15T11:17:11+00:00","dateModified":"2021-06-14T09:41:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/"},"wordCount":469,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg","keywords":["access nested dictionary python","accessing a nested dictionary python","convert a nested list into a dictionary lname python","convert nested dictionary to list python","create nested dictionary python","how to access nested dictionary in python","how to create a nested dictionary in python","initialize nested dictionary python","iterate over a nested dictionary python","nested dictionary python","print nested dictionary python","python create nested dictionary from csv","python get all values from nested dictionary","python nested dictionary","python nested dictionary access","python update nested dictionary"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/nested-dictionary-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/","url":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/","name":"Nested Dictionary in Python: Storing Data Made Easy - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg","datePublished":"2020-07-15T11:17:11+00:00","dateModified":"2021-06-14T09:41:42+00:00","description":"To store information in a structured way we can use nested dictionary in python. It is also known as a dictionary inside a dictionary.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/nested-dictionary-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-16.jpg","width":1002,"height":606,"caption":"nested dictionary python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/nested-dictionary-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Nested Dictionary in Python: Storing Data Made Easy"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056","name":"Ashwini Mandani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","caption":"Ashwini Mandani"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3595"}],"version-history":[{"count":9,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3595\/revisions"}],"predecessor-version":[{"id":13527,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3595\/revisions\/13527"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/3603"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}