Sometimes, we want to find the index of an item in a list with Python.
In this article, we’ll look at how to find the index of an item in a list with Python.
How to find the index of an item in a list with Python?
To find the index of an item in a list with Python, we can use the index
method available in a Python array.
For instance, we write:
index = ["foo", "bar", "baz"].index("bar")
print(index)
We call index
with the value we’re looking for.
And the index of the first element with the given value is returned.
Therefore index
is 1.
Conclusion
To find the index of an item in a list with Python, we can use the index
method available in a Python array.