[Python] How to iterate through a nested dict?

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have a nested python dictionary data structure. I want to read its keys and values without using collection module. The data structure is like bellow.

    d = {'dict1': {'foo': 1, 'bar': 2}, 'dict2': {'baz': 3, 'quux': 4}}


    I was trying to read the keys in the dictionary using the bellow way but getting error.

    Code

    for key, value in d:
    print(Key)


    Error

    ValueError: too many values to unpack (expected 2)


    So can anyone please explain the reason behind the error and how to iterate through the dictionary.

    Continue reading...

Compartilhe esta Página