{"id":5263,"date":"2021-05-07T11:14:13","date_gmt":"2021-05-07T05:44:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=5263"},"modified":"2021-11-22T18:42:54","modified_gmt":"2021-11-22T13:12:54","slug":"python-iterate-loop-over-all-nested-dictionary-values","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-iterate-loop-over-all-nested-dictionary-values\/","title":{"rendered":"Python: Iterate\/Loop over all Nested Dictionary values"},"content":{"rendered":"
Dictionaries are Python\u2019s implementation of an associative list, which\u00a0may be a\u00a0<\/span>arrangement\u00a0<\/span>. A dictionary\u00a0may be a\u00a0<\/span>collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.<\/p>\n To an infinite depth, a dictionary may include another dictionary, which can contain dictionaries, and so on. This is referred to as a nested dictionary.<\/p>\n Nested dictionaries are\u00a0one among\u00a0<\/span>several ways\u00a0during which\u00a0<\/span>structured information is represented (similar to records or structures in other languages).<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can only call the values() function of a dictionary for a normal dictionary to get an iterable sequence of values. However, a value can be another dictionary object in a nested dictionary. To do this, we need to call the values() function again and get a new iterative sequence of values and search for dictation objects in those values. This is possible with recurrence in a simple way.<\/p>\n To print all of the values in this dictionary, we wrote a function that will recursively go through the nested dictionary and print all of the values.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can convert nested dictionary values to list by taking an empty list and appending all values to list as shown below.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Dictionaries are Python\u2019s implementation of an associative list, which\u00a0may be a\u00a0arrangement\u00a0. A dictionary\u00a0may be a\u00a0collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair. To an infinite depth, a dictionary may include another dictionary, which can contain dictionaries, and so on. This is referred to as …<\/p>\nnesteddict = {\r\n'hello': {'www': 100, 'yyy': 'Helloworld'},\r\n'this': {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},\r\n'BTechGeeks': {'PLACE': 'HYDERABAD', 'PINCODE': 500000,\r\n'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},\r\n}<\/pre>\n
100\r\nHelloworld\r\nvikram\r\n20\r\nFEB\r\nHYDERABAD\r\n500000\r\nBuilt in\r\n1999<\/pre>\n
Traverse over all Nested Dictionary<\/h2>\n
\n
1)Traverse the values of nested Dictionary<\/h3>\n
# function which print all the values of nested dictionary\r\n\r\n\r\ndef printNested(nesteddict):\r\n # Traverse the dictionary\r\n for key, value in nesteddict.items():\r\n # If the value is of the dictionary type, then print\r\n # all\u00a0of\u00a0the\u00a0values\u00a0within\u00a0the\u00a0nested\u00a0dictionary.\r\n if isinstance(value, dict):\r\n printNested(value)\r\n else:\r\n print(value)\r\n\r\n\r\n# given nested_dictionary\r\nnesteddict = {\r\n 'hello': {'www': 100, 'yyy': 'Helloworld'},\r\n 'this': {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},\r\n 'BTechGeeks': {'PLACE': 'HYDERABAD', 'PINCODE': 500000,\r\n 'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},\r\n}\r\n# passing nested dictionary to printNested Function\r\nprintNested(nesteddict)\r\n<\/pre>\n
100\r\nHelloworld\r\nvikram\r\n20\r\nFEB\r\nHYDERABAD\r\n500000\r\nBuilt in\r\n1999<\/pre>\n
2)Convert all nested dictionary values to list<\/h3>\n
# function which print all the values of nested dictionary\r\n\r\n\r\ndef printNested(newlist, nesteddict):\r\n # Traverse the dictionary\r\n for key, value in nesteddict.items():\r\n # If the value is of the dictionary type, then print\r\n # all\u00a0of\u00a0the\u00a0values\u00a0within\u00a0the\u00a0nested\u00a0dictionary.\r\n if isinstance(value, dict):\r\n printNested(newlist, value)\r\n else:\r\n newlist.append(value)\r\n return newlist\r\n\r\n\r\n# given nested_dictionary\r\nnesteddict = {\r\n 'hello': {'www': 100, 'yyy': 'Helloworld'},\r\n 'this': {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},\r\n 'BTechGeeks': {'PLACE': 'HYDERABAD', 'PINCODE': 500000,\r\n 'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},\r\n}\r\nnewlist = []\r\n# passing nested dictionary to printNested Function\r\nprint(printNested(newlist, nesteddict))\r\n<\/pre>\n
[100, 'Helloworld', 'vikram', 20, 'FEB', 'HYDERABAD', 500000, 'Built in', 1999]<\/pre>\n
\n