Dictionary keys match on list; get key/value pair

后端 未结 6 2244
栀梦
栀梦 2021-02-12 14:55

In python... I have a list of elements \'my_list\', and a dictionary \'my_dict\' where some keys match in \'my_list\'.

I would like to search the dictionary and retrieve

6条回答
  •  情歌与酒
    2021-02-12 15:35

     new_dict = dict((k, v) for k, v in dict.iteritems() if k in list)
    

    Turning list into a set set(list) may yield a noticeable speed increase

提交回复
热议问题