Python Indexing a sublist

In this tutorial, we will learn how to find the index of a sublist that contains a specific element. This is useful when working with nested lists where you need to locate which sublist contains your target element.

Problem Statement

Given a nested list, we need to find the index of the sublist that contains a specific element ?

nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

Expected output for elements 7, 5, and 3 ?

Index of 7: 2
Index of 5: 1
Index of 3: 0

Using Nested Loops

The most straightforward approach uses nested loops to iterate through the main list and each sublist ?

# Initialize the nested list
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

# Function to find the index of sublist containing element
def find_sublist_index(element):
    # Iterate over the main list using index
    for i in range(len(nested_list)):
        # Check if element exists in current sublist
        if element in nested_list[i]:
            return i
    # Return -1 if element not found
    return -1

# Test the function
print(f"Index of 7: {find_sublist_index(7)}")
print(f"Index of 5: {find_sublist_index(5)}")
print(f"Index of 3: {find_sublist_index(3)}")
print(f"Index of 10: {find_sublist_index(10)}")
Index of 7: 2
Index of 5: 1
Index of 3: 0
Index of 10: -1

Using enumerate() for Cleaner Code

We can make the code more Pythonic using enumerate() ?

nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

def find_sublist_index_enumerate(element):
    for index, sublist in enumerate(nested_list):
        if element in sublist:
            return index
    return -1

# Test with multiple elements
elements = [7, 5, 3, 10]
for element in elements:
    index = find_sublist_index_enumerate(element)
    if index != -1:
        print(f"Index of {element}: {index}")
    else:
        print(f"Element {element} not found")
Index of 7: 2
Index of 5: 1
Index of 3: 0
Element 10 not found

Using List Comprehension

For a more functional approach, we can use list comprehension with next() ?

nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

def find_sublist_index_comprehension(element):
    try:
        return next(i for i, sublist in enumerate(nested_list) if element in sublist)
    except StopIteration:
        return -1

# Test the function
test_elements = [7, 5, 3]
for element in test_elements:
    index = find_sublist_index_comprehension(element)
    print(f"Index of {element}: {index}")
Index of 7: 2
Index of 5: 1
Index of 3: 0

Comparison of Methods

Method Readability Performance Best For
Nested Loops Good Average Beginners
enumerate() Excellent Good General use
List Comprehension Good Good Functional programming

Conclusion

Finding sublist indices in nested lists can be accomplished using various methods. The enumerate() approach offers the best balance of readability and performance for most use cases.

Updated on: 2026-03-25T09:00:08+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements