6

If i have a list then i look up a element in list by:

alist=[ele1, ele2, ele3, ele4,ele5,...]
if ele3 in alist:
  print "found" 

Will in stop a search from alist at ele3 ? Or it will run though all remaining element to the end.

Thanks in advance!

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
thangnv
  • 83
  • 1
  • 6
  • 5
    Implementation defined, I suppose; but it would have to be a pretty brain-dead implementation not to return early. – Blorgbeard Aug 06 '14 at 03:51
  • 2
    This *is not* a duplicate of the suggested question. That question asks for the time complexity, but that doesn’t help, since no matter whether Python exits early, the worst-case complexity is O(n); knowing that it’s O(n) doesn’t tell you anything about whether Python exits early. – icktoofay Aug 06 '14 at 04:03

4 Answers4

15

Will in stop a search from alist at ele3 ?

Yes, the in operator on a list does a linear search with an early exit if the target is found. Also, it will by-pass the final comparison if the target object is identical to the object in the list.

Here's some tracer code that proves the result by making the comparisons visible:

class Int(int):
    'Make comparisons visible'
    def __cmp__(self, other):
        print 'Comparing %s to %d' % (self, other)
        return int.__cmp__(self, other)

ele1 = Int(1)
ele2 = Int(2)
ele3 = Int(3)
ele4 = Int(4)
ele5 = Int(5)

alist = [ele1, ele2, ele3, ele4, ele5]
if ele3 in alist:
  print "found" 

The output is:

Comparing 3 to 1
Comparing 3 to 2
found

Python translates the in operator in the expression ele3 in alist into a magic method call such as alist.__contains__(ele3). The list.__contains__() method works like this:

def __contains__(self, target):
    for element in self:
        if target is element or target == element:
            return True
    return False

Hope this makes the process crystal clear :-)

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
2

in stop searching the list once it finds the item.

Source

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 4
    You could stand to have [a source](http://hg.python.org/cpython/file/a36d469f31c1/Objects/listobject.c#l402) for that claim. – icktoofay Aug 06 '14 at 03:58
1

Your purpose of the condition if ele3 in alist: is, check ele3 exist in alist or not. Once it finds then there is no need to process other element.

Like if you have condition

False and True

if the first element is False then there is no need to process rest statement. Because what ever remaining statement is it will be always False.

Same rule apply to your condition, once find then there is no need to process other element.

Nilesh
  • 20,521
  • 16
  • 92
  • 148
1

Basically 'In' operator works just like this function:

def IsInEnumerable(element, enumerable):
    for e in enumerable:
        if e == element:
            return True

    return False

IsInEnumerable('test', ['foo', 'test', 'bar', 'meow', 'quack']) # True

As you see, there will be only two iterations of 'for' loop. Same is true when using 'in' operator.

rufanov
  • 3,266
  • 1
  • 23
  • 41