Filter dict to contain only certain keys?

后端 未结 15 1084
耶瑟儿~
耶瑟儿~ 2020-11-22 10:52

I\'ve got a dict that has a whole bunch of entries. I\'m only interested in a select few of them. Is there an easy way to prune all the other ones out?

15条回答
  •  攒了一身酷
    2020-11-22 11:27

    You could use python-benedict, it's a dict subclass.

    Installation: pip install python-benedict

    from benedict import benedict
    
    dict_you_want = benedict(your_dict).subset(keys=['firstname', 'lastname', 'email'])
    

    It's open-source on GitHub: https://github.com/fabiocaccamo/python-benedict


    Disclaimer: I'm the author of this library.

提交回复
热议问题