How to check if a list contains an item

Tim Chase python.list at tim.thechases.com
Sun Apr 12 21:38:02 CEST 2009


online.service.com at gmail.com wrote:
> python doesn't have a list.contains()   method?

Because it doesn't need one?

   >>> x = [1,2,3,4]
   >>> 2 in x
   True
   >>> 42 in x
   False


pedantic side-note, objects can have a __contains__ method which 
is used under the covers for "in" testing, so your custom objects 
can also test for containment.

-tkc






More information about the Python-list mailing list