How to iterate through a nested dict?

前端 未结 7 1010
谎友^
谎友^ 2020-12-08 23:50

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

7条回答
  •  囚心锁ツ
    2020-12-09 00:12

    you can iterate all keys and values of nested dictionary as following:

    d = {'dict1': {'foo': 1, 'bar': 2}, 'dict2': {'baz': 3, 'quux': 4}}
    
    for i in d:
        for j, k in d[i].items():
            print(j,"->", k)
    

    Your output looks like this -

    foo -> 1
    bar -> 2
    baz -> 3
    quux -> 4
    

提交回复
热议问题