Python: finding an element in a list
Python: finding an element in a list
From Dive Into Python:
>>> li
[a, b, new, mpilgrim, z, example, new, two, elements]
>>> li.index(example)
5
If you just want to find out if an element is contained in the list or not:
>>> li
[a, b, new, mpilgrim, z, example, new, two, elements]
>>> example in li
True
>>> damn in li
False
Python: finding an element in a list
The best way is probably to use the list method .index.
For the objects in the list, you can do something like:
def __eq__(self, other):
return self.Value == other.Value
with any special processing you need.
You can also use a for/in statement with enumerate(arr)
Example of finding the index of an item that has value > 100.
for index, item in enumerate(arr):
if item > 100:
return index, item