Python | Last occurrence of some element in a list

There are many ways to find out the first index of element in the list as Python in its language provides index() function that returns the index of first occurrence of element in list. But if one desires to get the last occurrence of element in list, usually a longer method has to be applied.

Let’s discuss certain shorthands to achieve this particular task.

Method #1 : Using join() + rfind()
This is usually the hack that we can employ to achieve this task. Joining the entire list and then employing string function rfind() to get the first element from right i.e last index of element in list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to get last element occurrence
# using join() + rfind()
  
# initializing list
test_list = ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r',
                            'g', 'e', 'e', 'k', 's']
  
# using join() + rfind()
# to get last element occurrence
res = ''.join(test_list).rindex('e')
  
# printing result
print ("The index of last element occurrence: " + str(res))

chevron_right


Output:

The index of last element occurrence: 10

 
Method #2 : Using List Slice + index()
Using list slicing we reverse the list and use the conventional index method to get the index of first occurrence of element. Due to the reversed list, the last occurrence is returned rather than the first index of list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to get last element occurrence
# using List Slice + index()
  
# initializing list
test_list = ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r',
                            'g', 'e', 'e', 'k', 's']
  
# using List Slice + index()
# to get last element occurrence
res = len(test_list) - 1 - test_list[::-1].index('e')
  
# printing result
print ("The index of last element occurrence: " + str(res))

chevron_right


Output:

The index of last element occurrence: 10

 
Method #3 : Using max() + enumerate()
We use enumerate function to get the list of all the elements having the particular element and then max() is employed to get max i.e last index of the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to get last element occurrence
# using max() + enumerate()
  
# initializing list
test_list = ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r',
                            'g', 'e', 'e', 'k', 's']
  
# using max() + enumerate()
# to get last element occurrence
res = max(idx for idx, val in enumerate(test_list) 
                                    if val == 'e')
  
# printing result
print ("The index of last element occurrence: " + str(res))

chevron_right


Output:

The index of last element occurrence: 10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.