Find Index in List of Lists in Python (3 Examples)

 

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

Here is an overview:

Let’s get into the Python code!

 

Create Example List of Lists

Here, we will create the example Python list of lists containing integer values whose index we will find in this tutorial. So, in your Python coding IDE, run the line of code below to create the example list of lists:

my_list = [[1, 2], [3, 4], [5, 6]]
 
print(my_list)
# [[1, 2], [3, 4], [5, 6]]
 
print(type(my_list))
# <class 'list'>

Now that we have created the example Python list of lists, we will now see examples of how to determine the index of the nested list.
 

Example 1: Get Index of List of Lists Using for Loop

In this first example, we will use a for loop to determine the index of the sublist containing the integer 6:

for i in range(len(my_list)):
    if 6 in my_list[i]:
        print(i)
# 2

In this method, a for loop is used to iterate through each sublist in the main list. Then, the if statement is used to check if the value we are looking for is in the current sublist. If the value is in the sublist, the method prints the index of the current sublist.

In this example, the code will output 2, since the value 6 is in the third sublist.
 

Example 2: Get Index of List of Lists Using List Comprehension

In this second example, we will use list comprehension to find the index of the sublist containing the integer 6:

index = [i for i in range(len(my_list)) if 6 in my_list[i]][0]
 
print(index)
# 2

This method uses list comprehension to create a list of indexes where the specified value appears in the main list. It uses the same if statement as in Example 1 to check if 6 is in any of the sublists. Then it appends the index of the first matching sublist to the new list called index.

The code will output 2, since the value 6 is in the third sublist.

 

Example 3: Get Index of List of Lists Using enumerate() Function & Nested for Loops

In this last example, we will use the enumerate() function and a nested for loops to get the indexes of the list of lists:

for i, sublist in enumerate(my_list):
    for j, item in enumerate(sublist):
        if item == 6:
            print(i, j)
 
# 2 1

In this example, the enumerate() function is used to iterate over both the indexes and the sublists in the main list. Then a nested loop is used to iterate over each value in each sublist and check if the value matches the one we are looking for. If the value matches, the method prints the index of the current sublist and the index of the current value within the sublist.

In this example, the code will output 2 and 1, since the value 6 is in the third sublist at index 1.

 
With that, we have demonstrated how to find the index in a list of lists in Python. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to find the index in a list of lists 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 in a list of lists 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 how to find the indexes of a list of lists in Python. 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