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.
# 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)) |
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.
# 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)) |
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.
# 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)) |
The index of last element occurrence: 10
Recommended Posts:
- Python | Element Occurrence in dictionary of list values
- Python | Sort tuple based on occurrence of first element
- Python | Count occurrence of all elements of list in a tuple
- Python | Count tuples occurrence in list of tuples
- Python | Replace elements in second list with index of same element in first list
- Python | Slicing list from Kth element to last element
- Python | How to get the last element of list
- Python | Three element sum in list
- Python | Operate on every Kth element of list
- Python | Removing first element of list
- Python | Element repetition in list
- Python | Operation to each element in list
- Python | Remove given element from the list
- Python | Add the element in the list with help of indexing
- Python | Move element to end of the list
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.



