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 key/value pairs for the keys matching the ‘my_list’ elements.
I tried this…
if any(x in my_dict for x in my_list):
print set(my_list)&set(my_dict)
But it doesn’t do the job.
(I renamed
listtomy_listanddicttomy_dictto avoid the conflict with the type names.)For better performance, you should iterate over the list and check for membership in the dictionary:
If you want to create a new dictionary from these key-value pairs, use