{"id":5336,"date":"2021-05-07T11:14:30","date_gmt":"2021-05-07T05:44:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=5336"},"modified":"2021-11-22T18:42:54","modified_gmt":"2021-11-22T13:12:54","slug":"python-how-to-iterate-over-nested-dictionary-dict-of-dicts","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-iterate-over-nested-dictionary-dict-of-dicts\/","title":{"rendered":"Python: How to Iterate over Nested Dictionary -Dict of Dicts"},"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

nesteddict = {\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}},<\/pre>\n

Output:<\/strong><\/p>\n

('hello', 'www', 100)\r\n('hello', 'yyy', 'Helloworld')\r\n('this', 'www', 'vikram')\r\n('this', 'age', 20)\r\n('this', 'DOB', 'FEB')\r\n('BTechGeeks', 'PLACE', 'HYDERABAD')\r\n('BTechGeeks', 'PINCODE', 500000)\r\n('BTechGeeks', 'PYTHON', 'FUNCTIONS', 'Built in')\r\n('BTechGeeks', 'PYTHON', 'year', 1999)<\/pre>\n

Traverse over Nested Dictionary<\/h2>\n

To get an iterable sequence of all key-value pairs in a normal dictionary, simply call the dictionary’s items() function. A value in a nested dictionary, on the other hand, can be another dictionary object. To accomplish this, we must call the items() function again on such values to obtain another iterable sequence of pairs and then search for dict objects within those pairs as well. Using recursion, we can accomplish all of this in a straightforward manner.<\/p>\n

We wrote a function that takes a dictionary as an argument and returns all key-value pairs contained within it. Internal \/ nested dictionary values were included. It returns a tuple containing all keys for that value in hierarchy for nested structures.<\/p>\n

Below is the implementation:<\/strong><\/p>\n

# function which print all the values of nested dictionary\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 of the keys and values within the nested dictionary.\r\n        if isinstance(value, dict):\r\n            for pair in printNested(value):\r\n                yield (key, *pair)\r\n        else:\r\n            yield(key, 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 using loop\r\nfor pair in printNested(nesteddict):\r\n    print(pair)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

('hello', 'www', 100)\r\n('hello', 'yyy', 'Helloworld')\r\n('this', 'www', 'vikram')\r\n('this', 'age', 20)\r\n('this', 'DOB', 'FEB')\r\n('BTechGeeks', 'PLACE', 'HYDERABAD')\r\n('BTechGeeks', 'PINCODE', 500000)\r\n('BTechGeeks', 'PYTHON', 'FUNCTIONS', 'Built in')\r\n('BTechGeeks', 'PYTHON', 'year', 1999)<\/pre>\n

Working of the above function:<\/strong><\/p>\n

We iterated over all the values in a dictionary of dictionaries using the function nested dict pair iterator() and printed each pair, including the parent keys.<\/p>\n

We iterated over all the key-value pairs of a given dictionary object within the function, and for each value object in the pair, we checked whether the value is of dict type or not. If not, it just returns the pair, but if value is a dict, it calls itself again with the dict value object as an argument and returns all of its key-value pairs. The process is repeated until all of the internal dictionary objects have not been covered. This is how a dictionary of dictionaries’ key-value pairs are generated.
\nRelated Programs<\/strong>:<\/p>\n