Find Index of List Element Conditionally in Python (3 Examples)
Hi! This tutorial will show you how to get the index of list element conditionally in the Python programming language.
Here is an overview:
Let’s get right into the Python code!
Create Demo Python List of Integers
Here, we will create the demo Python list of integers that we will use in the examples in this tutorial.
So, in your favorite Python IDE, run the line of code below to create the demo list of integers:
my_list = [1, 2, 3, 4, 5] print(my_list) # [1, 2, 3, 4, 5] print(type(my_list)) # <class 'list'>
Now that we have created the demo Python list, we will see examples of how we can conditionally determine the index of a list element.
Let’s create the search element variable whose index we will find using different methods:
search_element = 3
Example 1: Get Index of List Element Conditionally Using index() Method
In this first example, we will use Python’s index() method to get the index of the search element in the list:
try: index = my_list.index(search_element) except ValueError: index = None print(index) # 2
In this example, we used the
index() method of lists to find the index of the first occurrence of the search element in the list. This method returns the index of the element if it is found, and raises a ValueError exception if it is not found.
To handle the case where the search element is not found, we used a try-except block. The try block contains a call to
my_list.index(search_element), which attempts to find the index of the search element in the list.
If the element is found, the value of index is set to the returned index. If the element is not found, a ValueError exception is raised, which is caught by the except block.
In the except block, we set the value of index to “None” to indicate that the search element was not found in the list.
Example 2: Get Index of List Element Conditionally Using for Loop & Conditional “if” Statement
In this next example, we will use a for loop and a conditional “if” statement to get the index of the search element:
index = None for i in range(len(my_list)): if my_list[i] == search_element: index = i print(index) # 2
Here, we used a for loop to iterate through the indices of the list. The
range(len(my_list)) function returns a sequence of integers from 0 to the length of the list minus 1, which we’re using as the loop variable i.
Inside the loop, we used an if statement to check if the element at the current index is equal to the search element. If it is, we set the value of index to the current index and break out of the loop using the break keyword.
If the search element is not found in the list, the value of index remains None, since we initialize it to None before the loop.
Example 3: Get Index of List Element Conditionally Using List Comprehension & enumerate() Function
In this last example, we will use list comprehension and the enumerate() function to get the index of the search element:
index = [i for i, e in enumerate(my_list) if e == search_element] print(index) # 2
Here, we used a list comprehension with the
enumerate() function to generate a list of indices where the element is equal to the search element.
Next, we accessed the first element of the resulting list to get the index of the first occurrence of the search element in the list. Then, we printed the result.
So, with these examples, we have demonstrated how to conditionally find the index of a list element in Python. I hope you found this tutorial helpful.
Video, Further Resources & Summary
Do you need more explanations on how to conditionally find the index of a list element in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to conditionally find the index of a list element in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Access Elements in List within Dictionary in Python (2 Examples)
- count() Method for Lists in Python (2 Examples)
- Append to 2D List in Python (3 Examples)
- Transpose 2D List in Python (3 Examples)
- Learn Python Programming
This post has shown how to conditionally find the index of a list element in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter