Iterate Through 2D List in Python (2 Examples)

 

Hi! This short tutorial will demonstrate to you how to loop through a 2D list in the Python programming language.

Here is a quick overview:

Let’s get into the Python code!

 

Create Example 2D List

We will create the example 2D list of integers that will be iterated through in this tutorial. So, in your preferred Python IDE, run the line of code below to create the example 2D list:

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 the example 2D list created, we can now explore different ways to go through the items in the list.
 

Example 1: Loop Through 2D List Using Nested For Loop

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

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

In the above example, we used the first loop to access all the rows in the 2D list and then used the second loop to extract and print all the items in the rows.
 

Example 2: Loop Through 2D List Using List Comprehension

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

items = "\n".join(str(item) for row in my_list for item in row)
 
print(items) 
 
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9

In the example above, we used list comprehension to first iterate through all the rows in the 2D list and then looped through the items in rows and returned them all as strings using the str() function.

However, to print the output vertically, we wrote the list comprehension inside the join() method where we returned each value on a new line using the new line character "\n".

With that, we have demonstrated how to iterate through a 2D list in Python in this short tutorial. I do hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to iterate through 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 iterate through 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 iterate through 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