Open In App

How to Loop Three Level Nested Python Dictionary

We are given a three level nested Python Dictionary and our task is to loop through that three level nested Python dictionary. In this article, we will explore two different approaches Nested for loops and Recursion to Loop 3 Level Nested Dictionary Python.

Loop Three Level Nested Dictionary Python

Below are the possible approaches to Loop 3 Level Nested Dictionary in Python.



Loop 3 Level Nested Dictionary Using Nested for Loops

In this approach, we are using nested for loops to iterate through each level of the 3-level nested dictionary. The outermost loop (for key1, value1 in dict.items()) iterates over the first level, the middle loop (for key2, value2 in value1.items()) iterates over the second level, and the innermost loop (for key3, value3 in value2.items()) iterates over the third level.




dict = {
    'Geeks': {
        'for': {
            'Geeks': 1,
            'CS': 2,
            'IT': 3
        },
        'GFG': {
            'Courses': {
                'DSA': 4,
                'Algorithms': 5
            },
            'Quiz': 6
        }
    }
}
for key1, value1 in dict.items():
    print(f"Level 1: {key1}")
    for key2, value2 in value1.items():
        print(f"\tLevel 2: {key2}")
        for key3, value3 in value2.items():
            print(f"\t\tLevel 3: {key3} - {value3}")

Output

Level 1: Geeks
    Level 2: for
        Level 3: Geeks - 1
        Level 3: CS - 2
        Level 3: IT - 3
    Level 2: GFG
        Level 3: Courses - {'DSA': 4, 'Algorithms': 5}
        Level 3: Quiz - 6



Loop 3 Level Nested Dictionary Using Recursion

In this approach, we are using recursion to traverse and print each level of the 3-level nested dictionary. The recursiveFn function iterates through the key-value pairs, printing the current key with appropriate indentation based on the level. If the value is a nested dictionary, the function is called recursively with an incremented level. The base case is when the level is 3, at which point the entire dictionary content at that level is printed as a single entry.




def recursiveFn(d, level=1):
    for key, value in d.items():
        print('\t' * (level - 1) + f'Level {level}: {key}', end=' ')
        if isinstance(value, dict):
            print()
            if level == 3:
                print('\t' * level + f'- {value}')
            else:
                recursiveFn(value, level + 1)
        else:
            print(f'- {value}')
dict_data = {
    'Geeks': {
        'for': {
            'Geeks': 1,
            'CS': 2,
            'IT': 3
        },
        'GFG': {
            'Courses': {
                'DSA': 4,
                'Algorithms': 5
            },
            'Quiz': 6
        }
    }
}
recursiveFn(dict_data)

Output
Level 1: Geeks 
    Level 2: for 
        Level 3: Geeks - 1
        Level 3: CS - 2
        Level 3: IT - 3
    Level 2: GFG 
        Level 3: Courses 
            - {'DSA': 4, 'Algorithms': 5}
        Level 3: Quiz - 6



Article Tags :