Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python's `in` keyword perform a linear search? [duplicate]

How does Python checks if a given value exists in an iterable using in keyword. Does it perform a linear search? Like :

def naive(iterable, val):
    for i in range(len(l)):
        if iterable[i]==val:
            return True
    return False

? Or it has got a different way of doing that. Other than linear search?

like image 770
ShuklaSannidhya Avatar asked Mar 11 '13 03:03

ShuklaSannidhya


2 Answers

Python's in operator calls the __contains__ magic function on the container. That is implemented in different ways for different containers.

For strings, lists and tuples, it's a linear search (O(N)), though since it's implemented in C it will probably be faster than the pure-python one you have in your question.

For a sets and dicts, it's a hash-table lookup, which is much faster (O(1) average case).

Other containers will have different performance characteristics. I don't think there are any in the standard library, but a balanced tree data structure would probably be O(log N).

like image 163
Blckknght Avatar answered Oct 13 '22 02:10

Blckknght


The in keyword depends on the implementation of the __contains__ method in the object's class you are calling. That means for anything that isn't hashable (list, string) it performs a linear search but for a hashable data structures (dict, set) it would be amortized constant time.

like image 20
Jared Avatar answered Oct 13 '22 00:10

Jared