Find Index of Element in Nested List in Python (2 Examples)

 

Hi! This short tutorial will show you how to get the index of an element in a nested list in the Python programming language.

Here is an overview:

Let’s get right into the Python code!

 

Create Example Nested List

Here, we will create the example nested Python list of integers that we will use in this tutorial.

So, in your Python programming IDE, run the code below to create the example nested list:

my_list = [[4,5,6],[7,8,9]]
 
print(my_list)
 
# [[4, 5, 6], [7, 8, 9]]
 
print(type(my_list))
 
# <class 'list'>

With the example nested list created, we will now consider two ways to determine the index of an element in the nested list.

The element whose index we want to find is defined below:

target_element = 8

 

Example 1: Get Index of Element in Nested List Using List Comprehension

In this first example, we will use list comprehension to return the index of the target element in the nested list:

target_element_index = [[i, e.index(target_element)] for i, e in enumerate(my_list) if target_element in e][0]
 
print(target_element_index)
 
# [1, 1]
 
print(type(target_element_index))
 
# <class 'list'>

Here, we use the list comprehension with the enumerate() function to iterate over each sublist and its corresponding index in my_list.

Within the list comprehension, we check if target_element is present in the current sublist e.

If target_element is found in the sublist, it creates a new list containing the current index i and the index of the target element within the sublist e.index(target_element).

Finally, the [0] at the end of the list comprehension selects the first element from the resulting list. This assumes that target_element is present in at least one sublist.

If target_element is found, the variable target_element_index will be assigned a list with two values: the index of the sublist containing the target element and the index of the target element within that sublist.
 

Example 2: Get Index of Element in Nested List Using Nested for Loop

In this next example, we will use a nested for loop to determine the index of the target element in the nested list:

target_element_index = []
 
for i in range(len(my_list)):
  for j in range(len(my_list[i])):
    if my_list[i][j] == target_element:
      target_element_index.append([i,j])
 
print(target_element_index[0])
 
# [1, 1]
 
print(type(target_element_index))
 
# <class 'list'>

In the above example, we use nested loops to iterate through each element of my_list. The outer loop iterates over the indices of the outer list, while the inner loop iterates over the indices of the inner lists.

Within the loops, we check if the current element my_list[i][j] is equal to target_element. If a match is found, it appends a list containing the indices i and j to target_element_index using the append() method.

Finally, after the loops have finished executing, the first element of target_element_index is printed with the print() function, assuming that at least one match was found.

This element represents the indices of the first occurrence of the target element in the nested list.
 

Video, Further Resources & Summary

Do you need more explanations on how to find the index of an element in a nested list 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 find the index of an element in a nested list 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:

This post has shown, using two examples, how to find the index of an element in a nested list in Python. Your use case will determine which solution you will go adopt.

I hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top