How to iterate through a nested dict?

前端 未结 7 1029
谎友^
谎友^ 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:14

    Iterating through a dictionary only gives you the keys.

    You told python to expect a bunch of tuples, and it tried to unpack something that wasn't a tuple (your code is set up to expect each iterated item to be of the form (key,value), which was not the case (you were simply getting key on each iteration).

    You also tried to print Key, which is not the same as key, which would have led to a NameError.

    for key in d:
        print(key)
    

    should work.

提交回复
热议问题