>>> ["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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-find-indices-of-value-in-list
Python - Ways to find indices of value in list - GeeksforGeeks
July 11, 2025 - Python offers various methods to achieve this efficiently. Let’s explore them. List comprehension is an efficient way to find all the indices of a particular value in a list.
Top answer
1 of 16
6115
>>> ["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
725

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
Discussions

How to find the index of something in a list without knowing the full item
Loop through list, check if 'ham' in item. More on reddit.com
🌐 r/learnpython
7
1
March 29, 2021
How to know the index of an element in a list
>>> stuff = [['a', 'b'], ['c', 'd'], ['e', 'f']] >>> def find_index(a_list, target): for i,v in enumerate(a_list): if target in v: return i >>> find_index(stuff,'e') 2 More on reddit.com
🌐 r/learnpython
6
0
November 30, 2022
How to get index of element in list of lists
Your outer loop selects each sublist, your inner loop selects elements in each sublist. The string method e.index will throw an error if it doesn’t find what you’re looking for. It doesn’t seem like a terribly appropriate tool for the job here anyway, as you’re trying to find an e that is literally equal to ‘10’. So why not use ==? You could also use the list method .index and scrap the inner loop, but that’ll throw the same error if it doesn’t find what you’re looking for (so you’d have to do an in test first). More on reddit.com
🌐 r/learnpython
9
2
November 16, 2020
New to python, how can I get index of item in list?
I tried collection.index(33) I then get the error "must be str, not int" That would mean that collection isn't a list but something else. When I try with a list it works as expected: >>> collection = [1,2,3,4,5,6,7,8] >>> collection.index(1) 0 >>> collection.index(33) Traceback (most recent call last): File "", line 1, in ValueError: 33 is not in list Of course it would help if you supplied your code, preferrably uploaded to a text site like pastebin.com or gist.github.com. More on reddit.com
🌐 r/learnpython
5
7
December 13, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're ...
Published   April 27, 2025
🌐
W3Schools
w3schools.com › python › ref_list_index.asp
Python List index() Method
Python Examples Python Compiler ... Interview Q&A Python Bootcamp Python Training ... The index() method returns the position at the first occurrence of the specified value....
🌐
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 - Essentially, you want to say: "If during the iteration, the value at the given position is equal to 'Python', add that position to the new list I created earlier". You use the append() method for adding an element to a list. programming_languages = ["JavaScript","Python","Java","Python","C++","Python"] python_indices = [] for programming_language in range(len(programming_languages)): if programming_languages[programming_language] == "Python": python_indices.append(programming_language) print(python_indices) #output #[1, 3, 5]
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index()
The index() method returns the index of the specified element in the list.
🌐
TutorialsPoint
tutorialspoint.com › article › python-ways-to-find-indices-of-value-in-list
Python - Ways to find indices of value in list
March 25, 2026 - # initializing list numbers = [1, 3, 4, 3, 6, 7] print("Original list :", numbers) # using enumerate() to find indices for 3 indices = [i for i, value in enumerate(numbers) if value == 3] print("Indices of 3 :", indices)
Find elsewhere
🌐
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.
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - Python's built-in index() function is a useful tool for finding the index of a specific element in a sequence. This function takes an argument representing the value to search for and returns the index of the first occurrence of that value in the sequence.
🌐
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 - Otherwise, Python raises a ValueError. However, it will break your program if it is not handled correctly. ... Consider you are reading sensor readings and want to get the first occurrence of a specific type of reading. You should try catching it here so the program will not crash and produce an error if no such reading exists. sensor_readings = [50, 55, 60, 65, 70] def find_reading_index(reading, readings): try: return readings.index(reading) except ValueError: return "Reading not found in the list" result = find_reading_index(65, sensor_readings) print(result) result_not_found = find_reading_index(75, sensor_readings) print(result_not_found)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-get-the-indices-of-all-occurrences-of-an-element-in-a-list
Get the indices of all occurrences of an element in a list - Python - GeeksforGeeks
July 23, 2025 - If a match is found the index is added to indices. ... a = [3, 5, 3, 6, 3, 7] x = 3 ind = [] for i in range(len(a)): if a[i] == x: ind.append(i) print(ind) ... numpy library provides an efficient way to work with arrays and perform element-wise comparisons. This method is particularly useful for large datasets. ... import numpy as np a = [10, 20, 10, 30, 10, 40] x = 10 ind = np.where(np.array(a) == x)[0] print(list(ind))
🌐
datagy
datagy.io › home › python posts › python lists › python: find list index of all occurrences of an element
Python: Find List Index of All Occurrences of an Element • datagy
December 30, 2022 - In the following sections, you’ll ... positions of all occurrences of an element in a Python list is by using a for loop and the Python enumerate function....
🌐
TutorialsPoint
tutorialspoint.com › article › python-get-the-indices-of-all-occurrences-of-an-element-in-a-list
Python - Get the indices of all occurrences of an element in a list
March 27, 2026 - def get_indices(element, data): ... get_indices(element, my_numbers) print("Indices using index() method:", result) ... The enumerate() method with list comprehension is generally the most Pythonic and efficient approac...
🌐
Mahfuzur Rahman Khan
mr-khan.gitlab.io › python › list › 2020 › 06 › 04 › python-list-value-check-and-print-indices.html
How to check a value in list and finding indices of values in Python | Mahfuzur Rahman Khan
June 4, 2020 - # Python3 code to demonstrate # print list values with index # using enumerate() # initializing list test_list = [10, 20, 30, 40, 50, 60] # print from default 0 index print("print from default 0 index") [print(count, value) for count, value in enumerate(test_list)] # print from starting 100 index print("print from custom starting 100 index") [print(count, value) for count, value in enumerate(test_list, 100)] # Python3 code to demonstrate # finding indices of values # using enumerate() # initializing list test_list = [1, 3, 4, 3, 6, 7] print (f"Original list : {test_list}") # checking value 3 i
🌐
freeCodeCamp
freecodecamp.org › news › python-index-find-index-of-element-in-list
Python Index – How to Find the Index of an Element in a List
May 2, 2022 - For the fruits list, the valid list indices are 0, 1, 2 and 3. Let's do the reverse. That is, given a list item, let's find out the index or position of that item in the list. index = fruits.index('orange') #The value of index is 1 index = fruits.index('guava') #The value of index is 3 index = fruits.index('banana') #This raises a ValueError as banana is not present in list · Python lists provide us with the index method which allows us to get the index of the first occurrence of a list item, as shown above.
🌐
Reddit
reddit.com › r/learnpython › how to find the index of something in a list without knowing the full item
r/learnpython on Reddit: How to find the index of something in a list without knowing the full item
March 29, 2021 -

Hey everyone, I was wondering how I could find the index of an element without knowing the full element. For example, if I have the list of: [‘dog’, ‘cat’, ‘hamster’] and I don’t know what each of them are, but I know that one includes ‘ham’, how could I find the index? Thanks for any help.

🌐
Python Examples
pythonexamples.org › python-find-index-of-item-in-list
How to find index of an item in a list?
To find index of an item in a list, you can use list.index() method with the item passed as argument. In this tutorial, we will learn how to use list.index() method to find the index of specified element in this list, with well detailed examples.
🌐
Python Engineer
python-engineer.com › posts › find-index-of-item-in-list
How to find the index of an item in a List in Python - Python Engineer
To find the index of an item in Python lists, you can simply use my_list.index(): ... my_list = ["apple", "banana", "cherry"] my_list.index("banana") # --> 1 my_list.index("cherry") # --> 2 my_list.index("orange") # --> # ValueError: 'orange' ...
🌐
iO Flood
ioflood.com › blog › python-get-index-of-item-in-list
Python Get Index of Item In List | 3 Simple Methods
June 29, 2024 - Python list indices start from 0, so ‘apple’ is at index 0, ‘banana’ at index 1, and ‘cherry’ at index 2. The list.index() function takes one mandatory argument, which is the item you’re looking for.
🌐
ReqBin
reqbin.com › code › python › h54arbqc › python-list-index-example
How do I find the index of an element in a Python list?
In this Python List Index example, we get the index of a list item using the list.index() method. Below are more detailed examples of finding the index of an element in a Python list.