A list is an ordered collection of elements, and each element has an index starting from 0. So, the first element has an index of 0, the second has 1, the third has 2, and so on.
Here are three ways to find the index of an element in a Python List:
- Using list.index()
- Using enumerate() and list comprehension
- Using for loop
Method 1: Using list.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.
Visual Representation
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.
Example
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 index() will stop searching after index 2.
Pros
- It works efficiently for the first occurrence.
- The index() is a built-in method. No extra package is required.
Cons
- It does not return all occurrences for duplicates.
- By default, it requires error-handling mechanisms like try/catch. You can use the “in” operator to avoid it.
Using the “in” operator to avoid try…catch
To avoid potential errors related to the existence of 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
Method 2: 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 enumerate() function.
Visual Representation
The above figure shows that we have to find the index of element 19 in the list. As we can see, element 19 is located twice in the list, index 2 and index 4, so it returns 2 and 4 as a list.
Example
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]
Pros
- It finds all indices, so it handles duplicates gracefully.
- The syntax is readable and concise. Perfect pythonic way.
Cons
- This approach can be an overkill if you only need the first occurrence.
- It creates a list of all indices upfront (inefficient for huge lists with many matches).
Method 3: Using for loop with enumerate()
You can use a for loop with the enumerate() function to iterate through a list for a specific element and provide its index if it is found.
Example
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
Pros
- It provides a clean way to handle if 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.
Cons
- It is not ideal for multiple occurrences.
- It is less pythonic.
- It is less flexible than list comprehension.
That’s all!