Last modified: Mar 25, 2026 By Alexander Williams

2D Arrays in Python: Guide & Examples

A two-dimensional array is a grid of data. It has rows and columns. Think of it like a spreadsheet or a chessboard. In Python, we often use lists to create them.

This structure is vital for many tasks. You use it for game boards, image pixels, and scientific data. Learning 2D arrays opens doors to complex programming.

What is a 2D Array?

A 2D array is a list of lists. Each inner list represents a row. The elements are the columns. This creates a matrix-like structure.

It is different from a one-dimensional list. A 1D list is a simple sequence. A 2D list adds a second level of organization. This is key for grid-based logic.

Creating a 2D Array with Lists

The simplest way is using nested list comprehension. You define the number of rows and columns. Python fills it with initial values.


# Create a 3x3 2D array filled with zeros
rows = 3
cols = 4
matrix = [[0 for _ in range(cols)] for _ in range(rows)]
print(matrix)
    

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

You can also create it manually. This is good for small, fixed grids. Just write out the lists inside a list.


# Manual creation of a 2x3 array
manual_matrix = [[1, 2, 3], [4, 5, 6]]
print(manual_matrix)
    

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

Accessing Elements in a 2D Array

You use two indices. The first index is the row. The second index is the column. Remember, indexing starts at 0.


matrix = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
# Access the element in row 1, column 2
element = matrix[1][2]
print(f"Element at [1][2]: {element}")
    

Element at [1][2]: 60
    

To read an entire row, use one index. To read an entire column, you need a loop. This is a common operation.

Understanding Python Array Length is crucial here. The len() function helps find the number of rows. For columns, check the length of a row.

Modifying 2D Array Elements

Change a value by assigning a new one to its index. This is straightforward. You target the exact cell.


grid = [[1, 2], [3, 4]]
print("Original:", grid)
# Change the value at row 0, column 1
grid[0][1] = 99
print("Modified:", grid)
    

Original: [[1, 2], [3, 4]]
Modified: [[1, 99], [3, 4]]
    

Iterating Over a 2D Array

Use nested for loops. The outer loop goes through rows. The inner loop goes through columns.


data = [[1, 2, 3], [4, 5, 6]]
for i in range(len(data)): # Iterate rows
    for j in range(len(data[i])): # Iterate columns
        print(f"data[{i}][{j}] = {data[i][j]}")
    

data[0][0] = 1
data[0][1] = 2
data[0][2] = 3
data[1][0] = 4
data[1][1] = 5
data[1][2] = 6
    

You can also use `enumerate`. This gives you the index and the value. It's a cleaner approach.

Using NumPy for Powerful 2D Arrays

For numerical work, use NumPy. It is a specialized library. It offers speed and many functions.

First, install NumPy with `pip install numpy`. Then import it. Create arrays with the numpy.array() function.


import numpy as np
# Create a 2D NumPy array
np_array = np.array([[1, 2, 3], [4, 5, 6]])
print("NumPy Array:")
print(np_array)
print(f"Shape: {np_array.shape}") # Shows (rows, columns)
    

NumPy Array:
[[1 2 3]
 [4 5 6]]
Shape: (2, 3)
    

NumPy makes operations easy. You can add, multiply, or transpose entire matrices. Managing Python Array Size is simpler with the `.shape` attribute.

Common Operations and Examples

Let's look at practical tasks. These are things you will do often.

Find the sum of all elements. Use nested loops or NumPy.


# Using loops
list_2d = [[1, 2], [3, 4]]
total = 0
for row in list_2d:
    for val in row:
        total += val
print(f"Sum (loops): {total}")

# Using NumPy
import numpy as np
np_2d = np.array(list_2d)
print(f"Sum (NumPy): {np.sum(np_2d)}")
    

Sum (loops): 10
Sum (NumPy): 10
    

Transpose a matrix. Swap rows and columns. NumPy has a .T property.


matrix = [[1, 2, 3], [4, 5, 6]]
# Manual transpose
transposed = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
print("Manual Transpose:", transposed)

# NumPy transpose
np_matrix = np.array(matrix)
print("NumPy Transpose:")
print(np_matrix.T)
    

Manual Transpose: [[1, 4], [2, 5], [3, 6]]
NumPy Transpose:
[[1 4]
 [2 5]
 [3 6]]
    

For a complete Python Array Implementation Guide, explore more methods and best practices.

When to Use Lists vs. NumPy

Use basic lists for simple grids. It's good for learning and small tasks. No extra installation is needed.

Use NumPy for math and science. It is faster for large arrays. It has built-in functions for linear algebra.

Choose lists for flexibility and simplicity.Choose NumPy for performance and numerical operations.

Conclusion

Two-dimensional arrays are essential in Python. You can build them with nested lists. For advanced work, the NumPy library is the best choice.

Start by practicing creation and access. Move on to iteration and modification. Then explore NumPy for powerful features.

Master this concept. It will help you in data science, game development, and more. Keep your code clean and your logic clear.