Create a 2D List in Python

In Python, a 2D list (also called a nested list or matrix) is a list of lists, where each inner list represents a row of the matrix. You can create a 2D list using nested lists, list comprehensions, or the numpy library. Below are multiple ways to create a 2D list in Python with explanations.


Examples

1. Creating a 2D List Using Nested Lists

A simple way to create a 2D list is by defining a list of lists manually.

</>
Copy
# Creating a 2D list (3x3 matrix)
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Printing the 2D list
for row in matrix:
    print(row)

Explanation: Here, matrix is a list containing three inner lists, each representing a row. Each row contains three elements, forming a 3×3 matrix. The for loop iterates over the outer list and prints each row separately.

Output:

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

2. Creating a 2D List Using List Comprehension

We can use list comprehension to create a 2D list dynamically.

</>
Copy
# Creating a 3x3 matrix initialized with zeros
rows, cols = 3, 3
matrix = [[0 for _ in range(cols)] for _ in range(rows)]

# Printing the 2D list
for row in matrix:
    print(row)

Explanation: This code creates a 3×3 matrix filled with zeros. The list comprehension [[0 for _ in range(cols)] for _ in range(rows)] generates a list of lists dynamically. The outer loop runs for rows times, and the inner loop runs for cols times, initializing each element with 0.

Output:

[0, 0, 0]
[0, 0, 0]
[0, 0, 0]

3. Creating a 2D List Using a Loop

We can use nested for loop to initialize a 2D list dynamically.

</>
Copy
# Creating a 3x3 matrix filled with incremental numbers
rows, cols = 3, 3
matrix = []

# Initializing matrix using nested loops
for i in range(rows):
    row = []
    for j in range(cols):
        row.append(i * cols + j + 1)
    matrix.append(row)

# Printing the 2D list
for row in matrix:
    print(row)

Explanation: This code dynamically creates a 3×3 matrix where each element increases sequentially. The outer loop (for i in range(rows)) creates each row, while the inner loop (for j in range(cols)) fills each row with values. The formula i * cols + j + 1 ensures that numbers increase sequentially from 1.

Output:

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

4. Creating a 2D List Using NumPy

The numpy library provides an efficient way to create and manage 2D arrays.

</>
Copy
import numpy as np

# Creating a 3x3 matrix filled with ones
matrix = np.ones((3, 3), dtype=int)

# Printing the 2D list
print(matrix)

Explanation: Here, we use numpy.ones() to create a 3×3 matrix filled with ones. The dtype=int ensures that the matrix contains integer values. The advantage of using NumPy is that it provides better performance and additional functionalities compared to regular Python lists.

Output:

[[1 1 1]
 [1 1 1]
 [1 1 1]]

Conclusion

There are multiple ways to create a 2D list in Python:

  1. Nested Lists: Define a list of lists manually.
  2. List Comprehension: Efficiently generate a 2D list.
  3. Using Loops: Dynamically initialize a matrix.
  4. Using NumPy: Optimized for numerical computing.

For basic use cases, list comprehension is an efficient approach. However, for performance-heavy tasks, using NumPy is recommended.