×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Iterate Through a Nested List

Last Updated : May 03, 2025

In Python, a nested list is a list that contains other lists as its elements. Iterating through nested lists means accessing each sublist and then the elements within it. In this chapter, we will learn how to iterate through nested lists using different approaches with the help of examples.

Iterate Through Nested Lists Using Nested for Loops

You can iterate through a nested list is by using nested for loops where one for the outer list and another for the inner lists.

Example

In this example, we are iterating through a nested list of numbers and printing each number.

# Creating a nested list
numbers = [[1, 2], [3, 4], [5, 6]]

# Iterating using nested for loops
for sublist in numbers:
    for num in sublist:
        print(num)

The output of the above code would be:

1
2
3
4
5
6

Iterate Through Nested Lists Using Indexes

You can also use index values to access elements of a nested list. This method is useful when you need the position of each element.

Example

In this example, we are using indexes to print the row and column positions of each number in a nested list.

# Creating a nested list
matrix = [[10, 20], [30, 40], [50, 60]]

# Using index-based iteration
for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        print(f"Element at ({i}, {j}) is {matrix[i][j]}")

The output of the above code would be:

Element at (0, 0) is 10
Element at (0, 1) is 20
Element at (1, 0) is 30
Element at (1, 1) is 40
Element at (2, 0) is 50
Element at (2, 1) is 60

Iterate Through Nested Lists Using List Comprehension

List comprehension can be used to flatten a nested list by extracting all elements into a single list.

Example

In this example, we are using a list comprehension to flatten a 2D list into a 1D list.

# Creating a nested list
nested = [[7, 8], [9, 10], [11, 12]]

# Flattening the nested list using list comprehension
flattened = [item for sublist in nested for item in sublist]

# Displaying the flattened list
print(flattened)

The output of the above code would be:

[7, 8, 9, 10, 11, 12]

Iterate Through Nested Lists Using enumerate()

You can use enumerate() to access indexes of both outer and inner lists during iteration.

Example

In this example, we are printing the row and column indexes along with the elements of the nested list.

# Creating a nested list
grid = [[100, 200], [300, 400], [500, 600]]

# Using enumerate to get indexes
for i, row in enumerate(grid):
    for j, value in enumerate(row):
        print(f"Value at ({i}, {j}) is {value}")

The output of the above code would be:

Value at (0, 0) is 100
Value at (0, 1) is 200
Value at (1, 0) is 300
Value at (1, 1) is 400
Value at (2, 0) is 500
Value at (2, 1) is 600

Iterate Through Nested List with Conditional Logic

You can apply conditional logic while iterating through a nested list to filter specific elements.

Example

In this example, we are printing only even numbers from a nested list.

# Creating a nested list
numbers = [[13, 24], [35, 46], [57, 68]]

# Filtering even numbers
for sublist in numbers:
    for num in sublist:
        if num % 2 == 0:
            print(f"Even number found: {num}")

The output of the above code would be:

Even number found: 24
Even number found: 46
Even number found: 68

Exercise

Select the correct option to complete each statement about iterating through nested lists in Python.

  1. To access each sublist in a nested list, you typically use a ___ loop.
  2. To iterate through both the sublists and their elements, you use ___ loops.
  3. Given lst = [[1, 2], [3, 4]], the expression lst[1][0] returns ___.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.