Filter dict to contain only certain keys?

后端 未结 15 1161
耶瑟儿~
耶瑟儿~ 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:32

    Slightly more elegant dict comprehension:

    foodict = {k: v for k, v in mydict.items() if k.startswith('foo')}
    

提交回复
热议问题