Categories
indexing list python

Finding the index of an item in a list

4095

Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index 1?

3

  • 14

    Are you returning: [1] The lowest index in case there are multiple instances of "bar", [2] All the indices of "bar"?

    May 12, 2018 at 20:56


  • 6

    a) Is it guaranteed that item is in the list, or else how we should handle the error case? (return None/ raise ValueError) b) Are list entries guaranteed to be unique, and should we return the first index of a match, or all indexes?

    – smci

    May 21, 2018 at 6:20

  • 1

    View the answers with numpy integration, numpy arrays are far more efficient than Python lists. If the list is short it’s no problem making a copy of it from a Python list, if it isn’t then perhaps you should consider storing the elements in numpy array in the first place.

    Jan 28, 2020 at 12:21


5620

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

Reference: Data Structures > More on Lists

Caveats follow

Note that while this is perhaps the cleanest way to answer the question as asked, index is a rather weak component of the list API, and I can’t remember the last time I used it in anger. It’s been pointed out to me in the comments that because this answer is heavily referenced, it should be made more complete. Some caveats about list.index follow. It is probably worth initially taking a look at the documentation for it:

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.

Linear time-complexity in list length

An index call checks every element of the list in order, until it finds a match. If your list is long, and you don’t know roughly where in the list it occurs, this search could become a bottleneck. In that case, you should consider a different data structure. Note that if you know roughly where to find the match, you can give index a hint. For instance, in this snippet, l.index(999_999, 999_990, 1_000_000) is roughly five orders of magnitude faster than straight l.index(999_999), because the former only has to search 10 entries, while the latter searches a million:

>>> 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
 

Only returns the index of the first match to its argument

A call to index searches through the list in order until it finds a match, and stops there. If you expect to need indices of more matches, you should use a list comprehension, or generator expression.

>>> [1, 1].index(1)
0
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
>>> g = (i for i, e in enumerate([1, 2, 1]) if e == 1)
>>> next(g)
0
>>> next(g)
2

Most places where I once would have used index, I now use a list comprehension or generator expression because they’re more generalizable. So if you’re considering reaching for index, take a look at these excellent Python features.

Throws if element not present in list

A call to index results in a ValueError if the item’s not present.

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

If the item might not be present in the list, you should either

  1. Check for it first with item in my_list (clean, readable approach), or
  2. Wrap the index call in a try/except block which catches ValueError (probably faster, at least when the list to search is long, and the item is usually present.)

8

  • 37

    index returns the first item whose value is “bar”. If “bar” exists twice at list, you’ll never find the key for the second “bar”. See documentation: docs.python.org/3/tutorial/datastructures.html

    – mpoletto

    Jan 30, 2018 at 4:51

  • 9

    If you’re only searching for one element (the first), I found that index() is just under 90% faster than list comprehension against lists of integers.

    – slybloty

    Sep 19, 2019 at 20:13

  • 2

    What data structure should be used if the list is very long?

    – izhang05

    Feb 22, 2020 at 20:36

  • @izhang: Some auxillary index, like an {element -> list_index} dict, if the elements are hashable, and the position in the list matters.

    Feb 24, 2020 at 4:30

  • sequence1 = sorted(sequence2, key=.sequence3.index) is a very handy idiom. You may use index more often if that’s in your repertoire.

    – Dvd Avins

    Feb 24, 2021 at 6:42

988

One thing that is really helpful in learning Python is to use the interactive help function:

>>> help(["foo", "bar", "baz"])
Help on list object:

class list(object)
 ...

 |
 |  index(...)
 |      L.index(value, [start, [stop]]) -> integer -- return first index of value
 |

which will often lead you to the method you are looking for.

2

  • 11

    bpython is a nice user-friendly way to read the docs in an interactive fashion.

    – goetzc

    Sep 22, 2019 at 18:09

  • 19

    @davidavr yes, but then the rest of us who just want to google it instead of scrolling through the help docs wouldn’t have this nice, central, ranked set of options. 🙂

    – cydonian

    Apr 6, 2020 at 23:42


672

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

3

  • 1

    Enumeration works better than the index-based methods for me, since I’m looking to gather the indices of strings using ‘startswith” , and I need to gather multiple occurrences. Or is there a way to use index with “startswith” that I couldn’t figure out

    Oct 26, 2017 at 19:15

  • 7

    In my hands, the enumerate version is consistently slightly faster. Some implementation details may have changed since the measurement above was posted.

    Nov 17, 2017 at 18:43

  • 4

    This was already answered since ’11: stackoverflow.com/questions/6294179/…

    – Cristik

    Feb 10, 2019 at 10:55