Python List Class __contains__ Method Functionality
Asked Answered
J

4

18

Does the __contains__ method of a list class check whether an object itself is an element of a list, or does it check whether the list contains an element equivalent to the given parameter?

Could you give me an example to demonstrate?

Jakie answered 18/4, 2012 at 13:19 Comment(0)
N
15
>>> a = [[]]
>>> b = []
>>> b in a
True
>>> b is a[0]
False

This proves that it is a value check (by default at least), not an identity check. Keep in mind though that a class can if desired override __contains__() to make it an identity check. But again, by default, no.

Newsstand answered 18/4, 2012 at 13:24 Comment(1)
No, Python performs the identity check first and then only checks the value if the identity check fails. See the example below.Dulcle
D
8

Python lists (and tuples) first check whether an object itself is an element of a list (using the is operator) and only if that is False then does it check whether the object is equal to an item in the list (using the == operator). You can see this by creating an object that is not equal to itself:

>>> class X:
...     def __eq__(self, other):
...             return False
... 
>>> x = X()
>>> x == x
False

However since x is x, __contains__ still recognises that this object is in a list

>>> x in [1, 'a', x, 7]
True

That is, a lists __contains__ method is equivalent to:

def __contains__(self, other):
    return any(other is item or other == item for item in self)
  
Dulcle answered 5/5, 2021 at 23:25 Comment(1)
And a quote from the official documentation to back this up: "For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y)."Longerich
L
0

It depends on the class how it does the check. For the builtin list it uses the == operator; otherwise you couldn't e.g. use 'something' in somelist safely.

To be more specific, it check if the item is equal to an item in the list - so internally it's most likely a hash(a) == hash(b) comparison; if the hashes are equal the objects itself are probably compared, too.

Lycurgus answered 18/4, 2012 at 13:21 Comment(2)
It won't be just a check of hashes - hashes can be equal without the values being equal.Kenon
This is wrong. I just defined a __eq__ method which has been (involuntarily) picked up by __contains__ or in. Therefore, it's most likely list implements a a.__eq__(b) comparison.Winshell
F
0

It checks the value

>>> x = 8888
>>> y = 8888
>>> list1 = [x]
>>> print(id(x))
140452322647920
>>> print(id(y))
140452322648016
>>> y in list1
True
Freebooter answered 24/4, 2021 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.