>>> ["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
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - One common and versatile structure ... lists. 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
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
Find the index of a list element in Python - Stack Overflow
How could I find a certain index as to where one of the elements is located at within sets. So I would like to find where (2.0, 2.0, 152) is located within the sets list, which is the 5th index. Is... More on stackoverflow.com
🌐 stackoverflow.com
Help with .index()? finding multiple instances of item
The easy way would be to just use enumerate on the list, and then manually iterate the list and find the indices yourself. More on reddit.com
🌐 r/learnpython
9
3
March 11, 2023
Find the index of the minimum value in a list?
You could try a.index(min(a)) More on reddit.com
🌐 r/learnpython
12
2
June 18, 2015
🌐
W3Schools
w3schools.com › python › ref_list_index.asp
Python List index() Method
Python Examples Python Compiler ... Plan Python Interview Q&A Python Training ... The index() method returns the position at the first occurrence of the specified value....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
Explanation: a.index("dog") searches for "dog" in the list and element is found at index 1, so 1 is returned.
Published: July 17, 2026
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index() (with Code Visualization)
models = ['Claude', 'ChatGPT', 'Gemini', 'ChatGPT'] # Search 'ChatGPT' from start to end index = models.index('ChatGPT') print(index) # Output: 1 # Search 'ChatGPT' from index 2 to end index = models.index('ChatGPT', 2) print(index) # Output: 3 # Search 'ChatGPT' from index 2 to index 3 (exclusive) index = models.index('ChatGPT', 2, 3) print(index) # ValueError: 'ChatGPT' is not in list · Note: Python also supports negative indexing and you can use negative start and end indices with index().
🌐
Sentry
sentry.io › sentry answers › python › get the index of a list item in python
Python Index of Item in List Using list.index() | Sentry
July 3, 2026 - Call list.index() to find the position of an element, handle ValueError for missing items, and use enumerate with a list comprehension for multiple matches
Find elsewhere
🌐
ReqBin
reqbin.com › code › python › h54arbqc › python-list-index-example
How do I find the index of an element in a Python list?
To find the index of an element in a Python list, you can use the list.index(element, start, end) method. The list.index() method takes an element as an argument and returns the index of the first occurrence of the matching element.
🌐
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 - You can give a value and find its index and in that way check the position it has within the list. For that, Python's built-in index() method is used as a search tool. ... .index() is the search method which takes three parameters.
🌐
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.
🌐
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.

🌐
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 - In Python, the index() method allows you to find the index of an item in a list. Built-in Types - Common Sequence Operations — Python 3.11.4 documentation How to use the index() method of a list Impl ...
🌐
Built In
builtin.com › software-engineering-perspectives › python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
The index() method in Python finds the first occurrence of a specific character or element in a string or list. If the character or element is found, its index value will be returned.
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-index-of-element-in-array-in-python
Find index of element in array in python - GeeksforGeeks
July 23, 2025 - We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array.
🌐
htmx
htmx.org › docs
</> htmx ~ Documentation
Like hx-swap-oob, partials execute before the main swap and targets are resolved relative to the triggering element using the full extended CSS selector vocabulary (closest, find, next, previous, etc.). See the hx-partial documentation for full details. If you want to select a subset of the response HTML to swap into the target, you can use the hx-select attribute, which takes a CSS selector and selects the matching elements from the response. You can also pick out pieces of content for an out-of-band swap by using the hx-select-oob attribute, which takes a list of element IDs to pick out and swap.
🌐
GitHub
github.com › jwasham › coding-interview-university
GitHub - jwasham/coding-interview-university: A complete computer science study plan to become a software engineer. · GitHub
Practice coding using arrays and pointers, and pointer math to jump to an index instead of using indexing. ... insert(index, item) - inserts item at index, shifts that index's value and trailing elements to the right ... Linked Lists CS50 Harvard University - this builds the intuition.
Author: jwasham
🌐
Python
docs.python.org › 3 › library › collections.html
collections — Container datatypes
Remove and return an element from the left side of the deque. If no elements are present, raises an IndexError. ... Remove the first occurrence of value. If not found, raises a ValueError. ... Reverse the elements of the deque in-place and then return None.
🌐
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))
🌐
LearnPython.com
learnpython.com › blog › python-get-index-of-item-list
How to Get the Index of an Item in a List in Python | LearnPython.com
Each item in a Python list has an associated index that may be used for accessing that item. We can use the index() method to find the index of an item.
🌐
PrepBytes
prepbytes.com › home › python › how to find index in python list
Finding Index in Python List
March 13, 2023 - Example 3 to find index of element in list python when the element not present then what will index function return: When an element is not present in the list and we use the index() function to find its index, a ValueError is raised.