How to iterate through a nested dict?

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

    You could use benedict (a dict subclass) and the traverse utility method:

    Installation: pip install python-benedict

    from benedict import benedict
    
    d = benedict({'dict1': {'foo': 1, 'bar': 2}, 'dict2': {'baz': 3, 'quux': 4}})
    
    def traverse_item(dct, key, value):
       print('key: {} - value: {}'.format(key, value))
    
    d.traverse(traverse_item)
    

    Documentation: https://github.com/fabiocaccamo/python-benedict

    Note: I am the author of this project.

    0 讨论(0)
提交回复
热议问题