How to check if an element exists in a Python array (Equivalent of PHP in_array)?

laurent picture laurent · Feb 7, 2013 · Viewed 27.2k times · Source

I'm new to Python and I'm looking for a standard function that would tell me if an element is present in an array. I found the index method but it throws an exception if the element is not found. I just need some simple function that would return true if the element is in the array or false if not.

Basically an equivalent to PHP in_array.

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Feb 7, 2013
>>> 1 in [0, 1, 2, 3, 4, 5]
True