>>> ["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
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
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
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
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
🌐
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   2 days ago
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index()
The index() method returns the index of the specified element in the list.
🌐
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....
🌐
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
2 weeks ago - mylist_extended = ["run", "hop", "bop", "hop"] indices = [index for index, element in enumerate(mylist_extended) if element == "hop"] print(indices) # will print [1, 3] If the element is not present in mylistextended, indices will be an empty list. ... Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski. SEE EPISODES · Change the order of columns in a Python Pandas DataFrame
Find elsewhere
🌐
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.

🌐
FavTutor
favtutor.com › blogs › get-list-index-python
Get the Index of an Element in a List in Python | FavTutor
1 day ago - 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.
🌐
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.
🌐
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 ...
🌐
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.
🌐
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.
🌐
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.
Starred by 357K users
Forked by 84.3K users
🌐
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.
🌐
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))
🌐
Django
docs.djangoproject.com › en › 6.0 › ref › models › fields
Model field reference | Django documentation | Django
The default is the project’s DEFAULT_INDEX_TABLESPACE setting, if set, or the db_tablespace of the model, if any. If the backend doesn’t support tablespaces for indexes, this option is ignored. ... The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created. The default can’t be a mutable object (model instance, list, set, etc.), as a reference to the same instance of that object would be used as the default value in all new model instances.
🌐
htmx
htmx.org › docs
</> htmx ~ Documentation
find <CSS selector> which will find the first child descendant element that matches the given CSS selector. (e.g find tr would target the first child descendant row to the element) In addition, a CSS selector may be wrapped in < and /> characters, mimicking the query literal syntax of hyperscript.
🌐
Sololearn
sololearn.com › en › Discuss › 2469798 › how-to-find-the-index-of-a-an-element-in-the-list-python
How to find the index of a an element in the list (python) | Sololearn: Learn to code for FREE!
python3index · 29th Aug 2020, 12:34 PM · Tayyib · 2 Answers · Answer · + 3 · I am assuming you mean finding the index of an element by name? If yes, there is the index() method ["foo", "bar", "baz"].index("bar") returns: 1 · 29th Aug ...
🌐
PrepBytes
prepbytes.com › home › python › how to find index in python list
Finding Index in Python List
March 13, 2023 - A: Yes, we can find the index of an element in a list multiple times, but the output of the index() function will only give the index of the first occurrence of the element in the list.