So I have a list of numbers in python in a list like this :
[1,89,1221,1919,1920,10210,...] with some thousands of numbers and i want to check if a variabele i is in it.
I do it this way :
if i in mylist:
But is that the fastest way?
Some further specs :
- the list holds no duplicates
- the list only holds integers
- the list can be ordered if that increases performance
- the list can in fact be a set (so no duplicates)
- the list can be any kind of collection
Converting to a set and doing
inis the fastest way.A set is basically a hash table, and lookups are O(1).