17

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?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
Jim West
  • 195
  • 1
  • 4

4 Answers4

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.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
  • 1
    No, Python performs the identity check first and then only checks the value if the identity check fails. See the example below. – Mark Bell May 05 '21 at 23:27
5

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 roughly equivalent to:

def __contains__(self, other):
    return any(other is item or other == item for item in self)
  
Mark Bell
  • 880
  • 1
  • 12
  • 22
  • And a quote from the [official documentation](https://docs.python.org/3/reference/expressions.html#membership-test-details) 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)`."_ – MestreLion Feb 14 '23 at 02:54
1

It checks the value

>>> x = 8888
>>> y = 8888
>>> list1 = [x]
>>> print(id(x))
140452322647920
>>> print(id(y))
140452322648016
>>> y in list1
True
Grabinuo
  • 334
  • 2
  • 11
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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    It won't be just a check of hashes - hashes can be equal without the values being equal. – Gareth Latty Apr 18 '12 at 13:25
  • 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. – Atcold Feb 13 '18 at 17:27