Just use the in keyword to check if a key is in the dictionary.
The following example will print [3, 1]
, since 3 and 1 are keys in the dictionary, as well as list items.
someList = [8, 9, 7, 3, 1] someDict = {1:2, 2:3, 3:4, 4:5, 5:6} intersection = [i for i in someList if i in someDict] print(intersection)
You can simply check if this intersection
list is empty at each iteration. If the list is empty, you know that no items in the list are keys in the dictionary.
source share