Categories
Python Answers

How to find the index of an item in a list with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *