Open In App

Last Occurrence of Some Element in a List – Python

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a list we need to find last occurrence of some elements in list. For example, w = [“apple”, “banana”, “orange”, “apple”, “grape”] we need to find last occurrence of string ‘apple’ so output will be 3 in this case.

Using rindex()

rindex() method in Python returns the index of the last occurrence of an element in a list. It searches the list from right to left ensuring that last index of the element is found.

Python
w = ["apple", "banana", "orange", "apple", "grape"]

# Use slicing to reverse the list, find the index of "apple", and adjust the index to the original list
ind = len(w) - 1 - w[::-1].index("apple")
print(ind)  

Output
3

Explanation:

  • w[::-1] reverses the list, and index(“apple”) finds the index of the first occurrence of “apple” in the reversed list, which corresponds to the last occurrence in the original list.
  • len(w) – 1 adjusts the reversed index back to the original list index, giving the position of the last “apple” element.

Using a Loop

We can iterate through the list from the end to the beginning, checking for the element. The index of first match we encounter will be last occurrence of the element in the original list.

Python
a = [1, 2, 3, 4, 2, 5, 2]
ind = -1

# Iterate through the list and update 'ind' with the index
for i in range(len(a)):
    if a[i] == 2:
        ind = i
print(ind) 

Output
6

Explanation:

  • Loop iterates through list updating ind each time element 2 is found ensuring that last occurrence is captured.
  • After loop completes ind holds index of last occurrence of 2 which is 6 in this case.

Using enumerate()

Using enumerate() allows us to loop through list while keeping track of both index and element. By checking for target element we can update index of its last occurrence efficiently.

Python
a = [1, 2, 3, 4, 2, 5, 2]

# Use enumerate() to get both index and value, and find the maximum index 
ind = max(i for i, v in enumerate(a) if v == 2)
print(ind)  

Output
6

Explanation:

  • enumerate(a) provides both index and value of each element and generator expression finds all indices where value is 2.
  • max() selects highest index from those matches ensuring we get last occurrence of 2 in list.

Using reversed()

Using reversed() allows us to iterate through list from end to beginning making it easy to find first occurrence of element in reversed list. index of this match will correspond to last occurrence in original list.

Python
a = [1, 2, 3, 4, 2, 5, 2]

# Use reversed() to iterate through the list 
ind = len(a) - 1 - next(i for i, v in enumerate(reversed(a)) if v == 2)
print(ind)  

Output
6

Explanation:

  • reversed(a) iterates over list from right to left and enumerate() provides index for reversed list.
  • next() retrieves index of first occurrence of 2 in reversed list and len(a) – 1 adjusts result to original list index


Next Article
Practice Tags :

Similar Reads