I am writing a bit of python code where I had to check if all values in list2 was present in list1, I did that by using set(list2).difference(list1) but that function was too slow with many items in the list.
So I was thinking that list1 could be a dictionary for fast lookup…
So I would like to find a fast way to determent if a list has an item that isn’t part of a dict
performance wise is there any difference between
d = {1: 1, 2:2, 3:3}
l = [3, 4, 5]
for n in l:
if not n in d:
do_stuff
vs
for n in l:
if not d[n]:
do_stuff
and please if both of these are rubbish and you know something much quicker, tell me.
Edit1: list1 or d can contain elements not in list2 but not the other way around.
A fast way to achieve what you want will be using
alland a generator comprehension.This will be advantageous in the case that some elements of list1 are not present in list2.
Some timing. In the case where all values in the first list are contained in the second:
If we look at the case where some values in the first list are not present in the second:
You can see that this method is equivalent in performance to the
issubsetin the first case and is faster in the second case as it will short circuit and obviates the need to construct 2 intermediate sets (only requiring one).Having one large list and one small lists demonstrates the benefit of the gencomp method: