-1

I want to know what is the search algorithm used for implementing the python in operator. For example:

   if num in some_large_list:
      do_something(num)

I notice that it seems to be slow on large lists , what is the algorithmic complexity of the in operator ? How does it scale with size of the list. Can we implement something better?

(I have asked how the in operator has been implemented in python, what search algorithm it uses , and can we do something better than that for larger lists)

Ijaz Ahmad
  • 11,198
  • 9
  • 53
  • 73
  • 2
    Do you mean the "in" operation as in the "check if contains operation"? Or do you mean the "for ... in ..." operation? – Others Dec 30 '15 at 16:12
  • The larger your list the more values it has to iterate over so it makes sense that it slows as your list grows – Padraic Cunningham Dec 30 '15 at 16:13
  • 1
    That is not an answer to my question and there is no such answer in the previous questions you mentioned in duplicates – Ijaz Ahmad Dec 30 '15 at 17:39

1 Answers1

0
for num in some_large_list:

This basically iterates over the elements in the list one by one from starting till end. So, This time will obviously increase for longer lists.


What I think is increasing your running time is the operations you are performing inside the loop

do_something(num)
Haris
  • 12,120
  • 6
  • 43
  • 70