Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to search a list in python

When you do something like "test" in a where a is a list does python do a sequential search on the list or does it create a hash table representation to optimize the lookup? In the application I need this for I'll be doing a lot of lookups on the list so would it be best to do something like b = set(a) and then "test" in b? Also note that the list of values I'll have won't have duplicate data and I don't actually care about the order it's in; I just need to be able to check for the existence of a value.

like image 907
Ian Burris Avatar asked May 13 '11 14:05

Ian Burris


People also ask

What is the fastest way to find an element in a list Python?

To find an element in the list, use the Python list index() method, The index() is an inbuilt Python method that searches for an item in the list and returns its index. The index() method finds the given element in the list and returns its position.

Are lookups faster with dictionaries or lists in Python?

Lookups are faster in dictionaries because Python implements them using hash tables. If we explain the difference by Big O concepts, dictionaries have constant time complexity, O(1) while lists have linear time complexity, O(n).

How fast is Python dictionary lookup?

Speed. Lookups in lists are O(n), lookups in dictionaries are amortized O(1), with regard to the number of items in the data structure.


3 Answers

Also note that the list of values I'll have won't have duplicate data and I don't actually care about the order it's in; I just need to be able to check for the existence of a value.

Don't use a list, use a set() instead. It has exactly the properties you want, including a blazing fast in test.

I've seen speedups of 20x and higher in places (mostly heavy number crunching) where one list was changed for a set.

like image 144
orlp Avatar answered Sep 22 '22 16:09

orlp


"test" in a with a list a will do a linear search. Setting up a hash table on the fly would be much more expensive than a linear search. "test" in b on the other hand will do an amoirtised O(1) hash look-up.

In the case you describe, there doesn't seem to be a reason to use a list over a set.

like image 31
Sven Marnach Avatar answered Sep 23 '22 16:09

Sven Marnach


I think it would be better to go with the set implementation. I know for a fact that sets have O(1) lookup time. I think lists take O(n) lookup time. But even if lists are also O(1) lookup, you lose nothing with switching to sets.

Further, sets don't allow duplicate values. This will make your program slightly more memory efficient as well

like image 36
inspectorG4dget Avatar answered Sep 22 '22 16:09

inspectorG4dget