Iterate Over a Nested List in Python

To iterate over a nested list in Python, you can use nested loops, list comprehensions, or built-in functions like itertools.chain(). Nested loops allow you to access inner lists and their elements, while list comprehensions provide a more concise approach. Let’s go through these different ways to iterate over a nested list with examples.


Examples

1. Iterate Over Nested List Using Nested Loops

One of the simplest ways to iterate over a nested list is by using nested for loops. This allows us to access each sublist and its elements.

</>
Copy
# Defining a nested list
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Iterating using nested loops
for sublist in nested_list:
    for item in sublist:
        print(item, end=" ")

Explanation:

Here, nested_list is a list of lists. We use a nested loop where:

  • sublist represents each inner list.
  • item iterates through each element in the inner list.
  • The end=" " argument ensures output is printed in a single line.

Output:

1 2 3 4 5 6 7 8 9

2. Iterate Over Nested List Using List Comprehension

List comprehension provides a concise way to flatten and iterate over a nested list.

</>
Copy
# Defining a nested list
nested_list = [[10, 20], [30, 40], [50, 60]]

# Using list comprehension
flattened = [item for sublist in nested_list for item in sublist]

# Printing the flattened list
print(flattened)

Explanation:

Here, we use list comprehension:

  • sublist iterates over each inner list.
  • item extracts elements from each inner list.
  • The result is stored in flattened, which is a single-level list.

Output:

[10, 20, 30, 40, 50, 60]

3. Iterate Over Nested List Using itertools.chain()

The itertools.chain() function efficiently flattens nested lists into a single iterable.

</>
Copy
import itertools

# Defining a nested list
nested_list = [[100, 200], [300, 400], [500, 600]]

# Using itertools.chain to flatten and iterate
flattened = list(itertools.chain(*nested_list))

# Printing the flattened list
print(flattened)

Explanation:

Here, we use itertools.chain():

  • *nested_list unpacks the inner lists.
  • itertools.chain() merges elements into a single iterable.
  • The result is converted into a list and stored in flattened.

Output:

[100, 200, 300, 400, 500, 600]

4. Iterate Over Nested List Using Recursion

Recursion is useful for deeply nested lists where the depth is unknown.

</>
Copy
# Function to recursively iterate over a nested list
def iterate_nested_list(nested_list):
    for element in nested_list:
        if isinstance(element, list):
            iterate_nested_list(element)  # Recursive call
        else:
            print(element, end=" ")

# Defining a deeply nested list
nested_list = [[1, [2, 3]], [[4, 5], [6, 7]], 8]

# Calling the function
iterate_nested_list(nested_list)

Explanation:

Here, the iterate_nested_list() function:

  • Loops through elements of nested_list.
  • If an element is a list, it calls itself recursively.
  • Otherwise, it prints the element.

Output:

1 2 3 4 5 6 7 8

Conclusion

There are multiple ways to iterate over a nested list in Python:

  1. Nested loops: Best for simple structures.
  2. List comprehension: Concise and efficient.
  3. itertools.chain(): Ideal for flattening lists.
  4. Recursion: Best for deeply nested lists.