Python Nested List Comprehension

Nested list comprehension in Python allows us to create complex lists in a concise and readable manner. It involves using one list comprehension inside another to generate multi-dimensional lists efficiently. This technique is especially useful when working with matrices, grids, or processing nested data structures.


Examples

1. Creating a 2D Matrix Using Nested List Comprehension

We can generate a 2D list (matrix) using nested list comprehension. Below is an example that creates a 3×3 matrix where each element is initialized with its row and column indices.

</>
Copy
# Creating a 3x3 matrix using nested list comprehension
matrix = [[row * 3 + col + 1 for col in range(3)] for row in range(3)]

# Printing the matrix
for row in matrix:
    print(row)

Explanation:

  1. for row in range(3): Iterates through 3 rows.
  2. for col in range(3): Iterates through 3 columns for each row.
  3. row * 3 + col + 1: Computes the matrix value using row and column indices.

Output:

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

2. Flattening a Nested List

We can use nested list comprehension to flatten a multi-dimensional list into a single list.

</>
Copy
# Nested list
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8]]

# Flattening the nested list using list comprehension
flattened_list = [num for sublist in nested_list for num in sublist]

# Printing the flattened list
print(flattened_list)

Explanation:

  1. for sublist in nested_list: Iterates through each sublist.
  2. for num in sublist: Extracts each number from the sublists.
  3. The final list contains all elements from the sublists in a single sequence.

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

3. Filtering Even Numbers in a Nested List

We can use nested list comprehension to filter only even numbers from a nested list.

</>
Copy
# Original nested list
nested_list = [[10, 15, 20], [25, 30, 35], [40, 45, 50]]

# Extracting only even numbers using list comprehension
even_numbers = [[num for num in sublist if num % 2 == 0] for sublist in nested_list]

# Printing the filtered list
print(even_numbers)

Explanation:

  1. for sublist in nested_list: Iterates through each sublist.
  2. for num in sublist if num % 2 == 0: Selects only even numbers.
  3. The resulting list contains only even numbers while preserving the nested structure.

Output:

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

4. Creating a Multiplication Table

We can use nested list comprehension to generate a multiplication table up to a certain number.

</>
Copy
# Generating a 5x5 multiplication table
multiplication_table = [[(row + 1) * (col + 1) for col in range(5)] for row in range(5)]

# Printing the multiplication table
for row in multiplication_table:
    print(row)

Explanation:

  1. for row in range(5): Iterates through 5 rows.
  2. for col in range(5): Iterates through 5 columns.
  3. (row + 1) * (col + 1): Computes the multiplication value.

Output:

[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[3, 6, 9, 12, 15]
[4, 8, 12, 16, 20]
[5, 10, 15, 20, 25]

Conclusion

Nested list comprehension in Python is a powerful and compact way to work with multi-dimensional lists. Here’s what we covered:

  1. Creating a 2D matrix using nested list comprehension.
  2. Flattening a nested list into a single list.
  3. Filtering even numbers from a nested list.
  4. Generating a multiplication table dynamically.