>>> ["foo", "bar", "baz"].index("bar")
1

See the documentation for the built-in .index() method of the list:

list.index(x[, start[, end]])

Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.

The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.

Caveats

Linear time-complexity in list length

An index call checks every element of the list in order, until it finds a match. If the list is long, and if there is no guarantee that the value will be near the beginning, this can slow down the code.

This problem can only be completely avoided by using a different data structure. However, if the element is known to be within a certain part of the list, the start and end parameters can be used to narrow the search.

For example:

>>> import timeit
>>> timeit.timeit('l.index(999_999)', setup='l = list(range(0, 1_000_000))', number=1000)
9.356267921015387
>>> timeit.timeit('l.index(999_999, 999_990, 1_000_000)', setup='l = list(range(0, 1_000_000))', number=1000)
0.0004404920036904514

The second call is orders of magnitude faster, because it only has to search through 10 elements, rather than all 1 million.

Only the index of the first match is returned

A call to index searches through the list in order until it finds a match, and stops there. If there could be more than one occurrence of the value, and all indices are needed, index cannot solve the problem:

>>> [1, 1].index(1) # the `1` index is not found.
0

Instead, use a list comprehension or generator expression to do the search, with enumerate to get indices:

>>> # A list comprehension gives a list of indices directly:
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
>>> # A generator comprehension gives us an iterable object...
>>> g = (i for i, e in enumerate([1, 2, 1]) if e == 1)
>>> # which can be used in a `for` loop, or manually iterated with `next`:
>>> next(g)
0
>>> next(g)
2

The list comprehension and generator expression techniques still work if there is only one match, and are more generalizable.

Raises an exception if there is no match

As noted in the documentation above, using .index will raise an exception if the searched-for value is not in the list:

>>> [1, 1].index(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 2 is not in list

If this is a concern, either explicitly check first using item in my_list, or handle the exception with try/except as appropriate.

The explicit check is simple and readable, but it must iterate the list a second time. See What is the EAFP principle in Python? for more guidance on this choice.

Answer from Alex Coventry on Stack Overflow
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index() (with Code Visualization)
The index() method returns the index of a specified item in the list. If there are multiple matching items, it returns the index of the first occurrence of the item. Here's a quick example.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
DSA Python · Data Science · NumPy ... of an element in a list. It searches the list from left to right and returns the index of the first matching occurrence....
Published: July 17, 2026
🌐
W3Schools
w3schools.com › python › ref_list_index.asp
Python List index() Method
Python Examples Python Compiler ... Interview Q&A Python Training ... The index() method returns the position at the first occurrence of the specified value....
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - In this tutorial, you will learn about the Python index() function. The index() method searches an element in the list and returns its position/index.
Top answer
1 of 16
6114
>>> ["foo", "bar", "baz"].index("bar")
1

See the documentation for the built-in .index() method of the list:

list.index(x[, start[, end]])

Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.

The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.

Caveats

Linear time-complexity in list length

An index call checks every element of the list in order, until it finds a match. If the list is long, and if there is no guarantee that the value will be near the beginning, this can slow down the code.

This problem can only be completely avoided by using a different data structure. However, if the element is known to be within a certain part of the list, the start and end parameters can be used to narrow the search.

For example:

>>> import timeit
>>> timeit.timeit('l.index(999_999)', setup='l = list(range(0, 1_000_000))', number=1000)
9.356267921015387
>>> timeit.timeit('l.index(999_999, 999_990, 1_000_000)', setup='l = list(range(0, 1_000_000))', number=1000)
0.0004404920036904514

The second call is orders of magnitude faster, because it only has to search through 10 elements, rather than all 1 million.

Only the index of the first match is returned

A call to index searches through the list in order until it finds a match, and stops there. If there could be more than one occurrence of the value, and all indices are needed, index cannot solve the problem:

>>> [1, 1].index(1) # the `1` index is not found.
0

Instead, use a list comprehension or generator expression to do the search, with enumerate to get indices:

>>> # A list comprehension gives a list of indices directly:
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
>>> # A generator comprehension gives us an iterable object...
>>> g = (i for i, e in enumerate([1, 2, 1]) if e == 1)
>>> # which can be used in a `for` loop, or manually iterated with `next`:
>>> next(g)
0
>>> next(g)
2

The list comprehension and generator expression techniques still work if there is only one match, and are more generalizable.

Raises an exception if there is no match

As noted in the documentation above, using .index will raise an exception if the searched-for value is not in the list:

>>> [1, 1].index(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 2 is not in list

If this is a concern, either explicitly check first using item in my_list, or handle the exception with try/except as appropriate.

The explicit check is simple and readable, but it must iterate the list a second time. See What is the EAFP principle in Python? for more guidance on this choice.

2 of 16
726

The majority of answers explain how to find a single index, but their methods do not return multiple indexes if the item is in the list multiple times. Use enumerate():

for i, j in enumerate(['foo', 'bar', 'baz']):
    if j == 'bar':
        print(i)

The index() function only returns the first occurrence, while enumerate() returns all occurrences.

As a list comprehension:

[i for i, j in enumerate(['foo', 'bar', 'baz']) if j == 'bar']

Here's also another small solution with itertools.count() (which is pretty much the same approach as enumerate):

from itertools import izip as zip, count # izip for maximum efficiency
[i for i, j in zip(count(), ['foo', 'bar', 'baz']) if j == 'bar']

This is more efficient for larger lists than using enumerate():

$ python -m timeit -s "from itertools import izip as zip, count" "[i for i, j in zip(count(), ['foo', 'bar', 'baz']*500) if j == 'bar']"
10000 loops, best of 3: 174 usec per loop
$ python -m timeit "[i for i, j in enumerate(['foo', 'bar', 'baz']*500) if j == 'bar']"
10000 loops, best of 3: 196 usec per loop
🌐
Career Karma
careerkarma.com › blog › python › python index: a step-by-step guide
Python Index: A Step-By-Step Guide | Career Karma
December 1, 2023 - A ValueError is returned if the specified item does exist in the list. You may want to find the index of a particular element within your data. For example, say you have a list of the top ten baked goods sold at a bakery. You may want to find out the position of Banana Cake in that list. That’s where the Python index() method comes in.
🌐
StrataScratch
stratascratch.com › blog › how-to-get-the-index-of-an-item-in-a-list-in-python
How to Get the Index of an Item in a List in Python - StrataScratch
September 6, 2024 - It searches the list and returns the index of the item to be found first. It is a straightforward yet effective way to know its proper location in your data. ... The index () value gives the position in the list where the item exists.
🌐
freeCodeCamp
freecodecamp.org › news › python-find-in-list-how-to-find-the-index-of-an-item-or-element-in-a-list
Python Find in List – How to Find the Index of an Item or Element in a List
February 24, 2022 - programming_languages = ["JavaScript","Python","Java","C++"] print(programming_languages.index("React")) #output #line 3, in <module> # print(programming_languages.index("React")) #ValueError: 'React' is not in list · If you try and search for an item but there is no match in the list you're searching through, Python will throw an error as the return value - specifically it will return a ValueError.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › python › lists › .index()
Python | Lists | .index() | Codecademy
June 11, 2025 - The .index() method is a built-in Python list method that returns the index position of the first occurrence of a specified element within a list.
🌐
Tutorialspoint
tutorialspoint.com › python › list_index.htm
Python List index() Method
Following is the syntax for the Python List index() method − ... This method returns the first index in the list at which the object is found.
🌐
Temp Mail
tempmail.us.com › temp mail › blog › python › locating an item's index in a python list
Locating an Item's Index in a Python List - Temp Mail
July 24, 2024 - The item "bar" is called with the .index() method in order to acquire its position given a list my_list including components such as ["foo", "bar", "baz"]. The method returns the item's index if it is in the list, and it prints the index after that.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Find the Index of an Item in a List in Python | note.nkmk.me
July 27, 2023 - Built-in Types - Common Sequence Operations — Python 3.11.4 documentation ... To find the index of an item in a list, specify the desired item as an argument to the index() method. This method returns the zero-based index of the item.
🌐
ReqBin
reqbin.com › code › python › h54arbqc › python-list-index-example
How do I find the index of an element in a Python list?
The list.index() method takes an element as an argument and returns the index of the first occurrence of the matching element. If the element is not found, a ValueError exception will be thrown.
🌐
FavTutor
favtutor.com › blogs › get-list-index-python
Get the Index of an Element in a List in Python | FavTutor
July 19, 2026 - In Python, you get the index of an element in a list with the index() method: my_list.index(value) returns the position of the first match.
🌐
Guru99
guru99.com › home › python › python list index() with example
Python List index() with Example
July 11, 2026 - 🔢 Does the Python list index() method support negative indexing? The value returned by index() is always a non-negative position counted from the start.
🌐
Tutorialspoint
tutorialspoint.com › python › python_finding_the_index_list_item.htm
Python Finding the Index of a List Item
The index() method of list class returns the index of first occurrence of the given item. The index() method returns an integer, representing the index of the first occurrence of the object.
🌐
Programiz
programiz.com › python-programming › methods › string › index
Python String index()
Online Python Online JavaScript ... Go Online Rust Online Scala Online Dart Online R Online Ruby ... The index() method returns the index of a substring inside the string (if found)....
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › index in python
Index Function in Python: Complete Guide
May 28, 2025 - Index of an element in a list Python operations support various search strategies. You can search entire lists or specific ranges. Optional parameters control search boundaries effectively. # Finding element indices with error handling def find_element_index(lst, element): """Safely find index of element in list""" try: index_position = lst.index(element) return index_position except ValueError: return -1 # Element not found # Example usage animals = ['cat', 'dog', 'bird', 'fish', 'cat'] # Finding existing elements dog_index = find_element_index(animals, 'dog') print(f"Dog found at index: {dog_index}") # 1 # Finding non-existing elements snake_index = find_element_index(animals, 'snake') print(f"Snake found at index: {snake_index}") # -1 # Finding with range specification cat_index = animals.index('cat', 2) # Search from index 2 print(f"Second cat at index: {cat_index}") # 4