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) Output3 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 LoopWe 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) Output6 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) Output6 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) Output6 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 Comment More infoAdvertise with us Next Article Last Occurrence of Some Element in a List - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Get the Last Element of List in Python In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list:Input: list = [1, 3, 34, 12, 6]Output: 6Explanation: Last element of the list l in the above example is 6.Let's explore various methods of doing it in Python:1. Using Neg 2 min read Get the indices of all occurrences of an element in a list - Python We are given a list and our task is to find all the indices where a particular element occurs. For example, if we have a list like [1, 2, 3, 2, 4, 2] and the element is 2, then the output will be [1, 3, 5] because 2 appears at these positions.Using List ComprehensionList comprehension allows for a c 2 min read Remove last K elements of list - Python Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python.Using list slicingList slicing is one of 3 min read Python | Remove given element from list of lists The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p 6 min read Get first and last elements of a list in Python The task of getting the first and last elements of a list in Python involves retrieving the initial and final values from a given list. For example, given a list [1, 5, 6, 7, 4], the first element is 1 and the last element is 4, resulting in [1, 4]. Using indexingThis is the most straightforward way 3 min read Like