Python: Iterate/Loop over all Nested Dictionary values

Dictionaries are Python’s implementation of an associative list, which may be a arrangement . A dictionary may be a collection 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 a nested dictionary.

Nested dictionaries are one among several ways during which structured information is represented (similar to records or structures in other languages).

Examples:

Input:

nesteddict = {
'hello': {'www': 100, 'yyy': 'Helloworld'},
'this': {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},
'BTechGeeks': {'PLACE': 'HYDERABAD', 'PINCODE': 500000,
'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},
}

Output:

100
Helloworld
vikram
20
FEB
HYDERABAD
500000
Built in
1999

Traverse over all Nested Dictionary

1)Traverse the values of nested Dictionary

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.

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.

Below is the implementation:

# function which print all the values of nested dictionary


def printNested(nesteddict):
    # Traverse the dictionary
    for key, value in nesteddict.items():
        # If the value is of the dictionary type, then print
        # all of the values within the nested dictionary.
        if isinstance(value, dict):
            printNested(value)
        else:
            print(value)


# given nested_dictionary
nesteddict = {
    'hello':    {'www': 100, 'yyy': 'Helloworld'},
    'this':    {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},
    'BTechGeeks':    {'PLACE': 'HYDERABAD', 'PINCODE': 500000,
                      'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},
}
# passing nested dictionary to printNested Function
printNested(nesteddict)

Output:

100
Helloworld
vikram
20
FEB
HYDERABAD
500000
Built in
1999

2)Convert all nested dictionary values to list

We can convert nested dictionary values to list by taking an empty list and appending all values to list as shown below.

Below is the implementation:

# function which print all the values of nested dictionary


def printNested(newlist, nesteddict):
    # Traverse the dictionary
    for key, value in nesteddict.items():
        # If the value is of the dictionary type, then print
        # all of the values within the nested dictionary.
        if isinstance(value, dict):
            printNested(newlist, value)
        else:
            newlist.append(value)
    return newlist


# given nested_dictionary
nesteddict = {
    'hello':    {'www': 100, 'yyy': 'Helloworld'},
    'this':    {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},
    'BTechGeeks':    {'PLACE': 'HYDERABAD', 'PINCODE': 500000,
                      'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},
}
newlist = []
# passing nested dictionary to printNested Function
print(printNested(newlist, nesteddict))

Output:

[100, 'Helloworld', 'vikram', 20, 'FEB', 'HYDERABAD', 500000, 'Built in', 1999]

Related Programs: