As a Python developer during a project for one of my USA clients, I had a requirement to iterate through a 2D array in Python. I discovered many ways to achieve this task. We will go through detailed examples to learn how to iterate through a 2D array in Python.
What are 2D Arrays in Python?
2D array in Python is a list of lists. Each element in the outer list represents a row, and each element within a row represents a column. Here’s an example of a 2D array:
students = [
['John', 'New York', 85],
['Emily', 'California', 92],
['Michael', 'Texas', 78],
['Jessica', 'Florida', 89]
]In this example, we have a 2D array students that stores information about students, including their names, states, and grades.
Check out setting an array element with a sequence error in Python
Access Specific Elements from Python Array
Sometimes, you may need to access specific elements in a 2D array. You can do this by using the row and column indices. Here’s an example:
print(students[1][0])
print(students[2][2]) Output:
Emily
78A screenshot of the executed example code is added below, you can have a look.

In this example, students[1][0] accesses the element at the second row and first column, “Emily”. Similarly, students[2][2] access the element of the third row and third column, “78”.
Read Python program to print the smallest element in an array
Method 1. Iterating Through a 2D Array
Iterate through a 2D array in Python, using nested loops. The outer loop iterates over the rows, while the inner loop iterates over the columns within each row. Here’s an example:
for row in students:
for item in row:
print(item, end=' ')
print()Output:
John New York 85
Emily California 92
Michael Texas 78
Jessica Florida 89A screenshot of the executed example code is added below, you can have a look.

In this example, the outer loop for row in students iterates over each row of the students array. The inner loop for item in row iterates over each item within a row. We print each item followed by a space, and after each row, we use print() it to move to the next line.
Check out How to Split a String into an Array in Python
Method 2. Using enumerate() for Iteration
Python provides the enumerate() function, that allows you to iterate over a sequence and retrieve the index and the value at each iteration. You can use enumerate() to iterate through a 2D array and access the row index and row elements. Here’s an example:
for row_index, row in enumerate(students):
print(f"Row {row_index + 1}:")
for item in row:
print(item, end=' ')
print()Output:
Row 1:
John New York 85
Row 2:
Emily California 92
Row 3:
Michael Texas 78
Row 4:
Jessica Florida 89A screenshot of the executed example code is added below, you can have a look.

In this example, enumerate(students) returns a tuple for each row, where the first element is the row index, and the second element is the row itself. We use row_index to print the row number, and then iterate over each item in the row using the inner loop.
Read 3D Arrays in Python
Method 3. Iterate Through Columns
If you need to iterate through the columns of a 2D array, you can use the zip() function along with the unpacking operator *. Here’s an example:
for column in zip(*students):
print(column)Output:
('John', 'Emily', 'Michael', 'Jessica')
('New York', 'California', 'Texas', 'Florida')
(85, 92, 78, 89)In this example, zip(*students) unpack the students array into separate arguments, which are then passed to the zip() function. The zip() function combines the corresponding elements from each row into tuples. We then iterate over each column using the for loop.
Check out How to Print Duplicate Elements in Array in Python
Modify Elements in a Python 2D Array
You can modify elements in a 2D array by accessing them using the row and column indices and assigning new values. Here’s an example:
students[2][2] = 80
print(students)Output:
[['John', 'New York', 85], ['Emily', 'California', 92], ['Michael', 'Texas', 80], ['Jessica', 'Florida', 89]]In this example, we modify the element at the third row and third column by assigning it a new value of 80. The updated 2D array is then printed.
Read How to Convert an Array to a Tuple in Python
Conclusion
In this tutorial, I have explained how to iterate through a 2D array in Python, I explained what are 2D arrays in Python, And how to access specific elements from Python Array. I discussed how to Iterate through a 2D array using nested loops, And also functions like enumerate() and zip() that can be helpful in certain conditions. I showed how to modify elements in a Python 2D array.
I hope this tutorial has helped you understand how to iterate through a 2D array in Python. Happy coding!
You may also like to read:
- How to Get Values from a JSON Array in Python
- Python repeat array n times
- How to Update an Array in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.