18

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
laurent
  • 88,262
  • 77
  • 290
  • 428
  • You don't even need a function, you just say `if "hello" in some_array: # do stuff!` Way simpler if you ask me. – Alex V Feb 07 '13 at 03:48
  • Be sure to always check the [docs](http://docs.python.org/2/library/), they usually have everything you need. – jackcogdill Feb 07 '13 at 03:59
  • I'm sure the docs have everything. But the problem is, when the keyword is just "in", you already need to know where to look for in the doc. – laurent Apr 03 '15 at 09:12

1 Answers1

45
>>> 1 in [0, 1, 2, 3, 4, 5]
True
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358