The most efficient and reliable way is to use the list.index() method as illustrated in the above figure. We are searching for element 19, and if it is found, it returns its index.
The index() function returns the index of the element’s first occurrence, regardless of whether the element appears twice.
If the value you are looking for does not appear in the list, it will return a ValueError. So, we need to handle the ValueError exception.
The above figure shows that we want to find the index of element 19. Since indexing starts with 0 in the list, element 19 appears to be located at index 2.
main_list = [11, 21, 19, 46] element_to_find = 19 try: index = main_list.index(element_to_find) print(f"{element_to_find} found at index {index}") except ValueError: print(f"{element_to_find} not found in the list") # Output: 19 found at index 2
Now, if element 19 appears twice in the list, it will still return the first index of the element because it returns only a single index, not a list of multiple indices.
main_list = [11, 21, 19, 46, 19] element_to_find = 19 try: index = main_list.index(element_to_find) print(f"{element_to_find} found at index {index}") except ValueError: print(f"{element_to_find} not found in the list") # 19 found at index 2
In the above code, you can see that element “19” occurs at positions 2 and 4, but in the output, we found only index 2 because the index() method stops searching after index 2.
To avoid potential errors related to the existence of an input value, we can use the “in” operator before using the “index()” method.
main_list = [11, 21, 19, 46] element_to_find = 50 if element_to_find in main_list: index = main_list.index(element_to_find) print(f"{element_to_find} found at index {index}") else: print(f"{element_to_find} not found in the list") # Output: 50 not found in the list
Alternate approaches
Here are two other ways:
- Using enumerate() and list comprehension
- Using a for loop
Approach 1: Using enumerate() and list comprehension
If the same element appears multiple times in the list and you want to find all the indices of multiple occurrences, you can use list comprehension with the enumerate() function.
The above figure shows that we have to find the index of element 19 in the list. As we can see, element 19 appears twice in the list, at indices 2 and 4, so it returns 2 and 4 as a list.
main_list = [11, 21, 19, 46, 19] # Element to find element_to_find = 19 # Use list comprehension with enumerate() to find indices indices = [index for index, item in enumerate( main_list) if item == element_to_find] if indices: print(f"{element_to_find} found at indices {indices}") else: print(f"{element_to_find} not found in the list") # Output: 19 found at indices [2, 4]
It finds all indices, so it handles duplicates gracefully. The syntax is readable and concise. Perfect Pythonic way.
However, this approach can be overkill if you only need the first occurrence. It creates a list of all indices upfront (inefficient for huge lists with many matches).
Approach 2: Using a for loop with enumerate()
You can use a for loop with the enumerate() function to iterate through a list, searching for a specific element and providing its index if found.
main_list = [11, 21, 19, 46] # Element to find element_to_find = 19 # Iterate through the list with a for loop for index, element in enumerate(main_list): if element == element_to_find: print(f"{element_to_find} found at index {index}") break # Stop the loop after finding the first occurrence else: print(f"{element_to_find} not found in the list") # Output: 19 found at index 2
It provides a clean way to handle the case where the element is not found in the list. You don’t need to use try…except blocks to handle the case where the element is absent.
However, it is not ideal for multiple occurrences, and it is less Pythonic.
That’s all!