Python: finding an element in a list

Himadri Choudhury picture Himadri Choudhury · Mar 3, 2009 · Viewed 728.9k times · Source

What is a good way to find the index of an element in a list in Python?
Note that the list may not be sorted.

Is there a way to specify what comparison operator to use?

Answer

Matt Howell picture Matt Howell · Mar 3, 2009

From Dive Into Python:

>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
>>> li.index("example")
5