Traverse 2D List in Python (2 Examples)

 

Hi! This short tutorial will show you how to go across a 2D list in the Python programming language.

Here is an overview:

Let’s jump into the Python code!

 

Create Demo 2D List

Here, we will create a demo 2D list of integer values that we will go across in this tutorial. So, in your preferred Python coding IDE, run the code below to create the demo 2D list of integer values:

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 
print(my_list)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 
print(type(my_list))
# <class 'list'>

Now that we have created the example 2D list of integer values, we will examine different ways to traverse the rows and columns of the 2D list.
 

Example 1: Go Across 2D List Using Nested for Loop

In this first example, we will go across the 2D list using a nested for loop:

for row in my_list:
  for col in row:
    print(col)
 
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9

Here, the first loop, for row in my_list, iterates over each row in the 2D list, one at a time. In other words, it selects each sublist within the larger list.

The second loop, for col in row, then iterates over each element in the selected row, one at a time. In other words, it selects each individual element within the current sublist. Finally, the output of each selected element is printed to the console using the print() function.

 

Example 2: Go Across 2D List Using List Comprehension

In this next example, we will use list comprehension to go through the 2D list:

[print(col) for row in my_list for col in row].pop()
 
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9

Here, the list comprehension iterates over each row in the list, and for each row, it iterates over each element, printing it to the console. This results in all elements being printed in order, row by row, from left to right.

The code also calls the pop() method on the resulting list to remove the last “None” value, which is returned by the print() function. This ensures that only the printed elements are left in the list, and not the “None” values.

 
So, with that, we have demonstrated, with two examples, how to traverse a 2D list in Python. I do hope you found this short tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to traverse a 2D list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to traverse a 2D list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to traverse a 2D list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top