Find index of last occurrence of item in a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Find index of last occurrence of item in a List in Python #

To find the index of the last occurrence of an item in a list:

  1. Use list slicing to reverse the list.
  2. Use the list.index() method to get the index of the item.
  3. Subtract the index from the list's length minus 1.
main.py
my_list = ['one', 'two', 'one', 'three', 'four'] index_last_occurrence = ( len(my_list) - 1 - my_list[::-1].index('one') ) print(index_last_occurrence) # 👉️ 2

We used the my_list[::-1] syntax to reverse the list.

main.py
my_list = ['one', 'two', 'one', 'three', 'four'] print(my_list[::-1]) # 👉️ ['four', 'three', 'one', 'two', 'one']

The syntax for list slicing is my_list[start:stop:step].

We only specified a value for the step of -1 to reverse the list.

We used the list.index() method to get the index of the item in the reversed list.

The last step is to subtract the index of the item from the list's length - 1.

main.py
my_list = ['one', 'two', 'one', 'three', 'four'] index_last_occurrence = ( len(my_list) - 1 - my_list[::-1].index('one') ) print(index_last_occurrence) # 👉️ 2

We had to subtract 1 from the list's length because indexes are zero-based in Python.

The first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

If you need to handle the scenario where the value is not contained in the list, use the ternary operator.

main.py
my_list = ['one', 'two', 'one', 'three', 'four'] item = 'abc' index_last_occurrence = ( len(my_list) - 1 - my_list[::-1].index(item) if item in my_list else None ) print(index_last_occurrence) # 👉️ None

If the value is contained in the list, we return the index of its last occurrence, otherwise we return None.

Alternatively, you can use the max() function.

Find index of last occurrence of item in a List using max() #

To find the index of the last occurrence of an item in a list:

  1. Use a generator expression to iterate over the list with enumerate().
  2. Return the index of each list item that is equal to the specified value.
  3. Use the max() function to get the index of the last occurrence of the item.
main.py
my_list = ['one', 'two', 'one', 'three', 'four'] index_last_occurrence = max( (index for index, item in enumerate(my_list) if item == 'one'), default=None ) print(index_last_occurrence) # 👉️ 2

We used a generator expression to iterate over the enumerate object.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 bobby, 1 hadz, 2 com

On each iteration, we check if the current item is equal to a specific value and return the current index if the condition is met.

The last step is to use the max() function to return the index of the last occurrence of the item in the list.

main.py
my_list = ['one', 'two', 'one', 'three', 'four'] index_last_occurrence = max( (index for index, item in enumerate(my_list) if item == 'one'), default=None ) print(index_last_occurrence) # 👉️ 2

The max function returns the largest item in an iterable or the largest of two or more arguments.

main.py
my_list = [15, 45, 30] result = max(my_list) print(result) # 👉️ 45

Notice that we set the default keyword argument to None in the call to max().

The default argument specifies a value to return if the provided iterable is empty.

If the value is not contained in the list, we return None.

main.py
my_list = ['one', 'two', 'one', 'three', 'four'] index_last_occurrence = max( (index for index, item in enumerate(my_list) if item == 'abc'), default=None ) print(index_last_occurrence) # 👉️ None

If the value is not contained in the list and a default argument is not provided, the max() function raises a ValueError.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.