Access Elements in 2D List in Python (3 Examples)


Hi! This tutorial will demonstrate how to retrieve the elements in a 2D list in the Python programming language.

Here is a quick overview:

Let’s dive into the Python code!


Create Example 2D List

Here, we will create the example 2D list of integers whose elements we are going to extract in this tutorial. So, in your preferred Python IDE, run the line of code below to create the demo 2D list:

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

With the example 2D list created, we are now ready to see examples of how to access its elements.

Example 1: Retrieve Single Row in 2D List

In this first example, we will retrieve a single row inside the 2D list:

# [4, 5, 6]

Python indexing starts at 0; therefore, in the example above, my_list[1] returned the second row inside the 2D list. Changing the index number to 2 will return the third row inside the 2D list.

Example 2: Retrieve Individual Elements in 2D List

In this example, we will return individual elements from the sublists inside the 2D list:

# 2
# 6
# 7

In the above example, we returned the second, third, and first elements in each sublists inside the 2D list. Remember that Python indexing starts from 0. Therefore, my_list[0] returns [1, 2, 3], which is the first element in the 2D list. To return the second element in the sublist, we write my_list[0][1].


Example 3: Retrieve Single Column in 2D List

In this final example, we will use the list comprehension method to extract the third column in the 2D list. First, though, let us take a look at the columns in the 2D list.

We will make use of a for loop, as this will return the data in a way that will make it easy to see the columns:

for i in my_list:
# [1, 2, 3]
# [4, 5, 6]
# [7, 8, 9]

Now that we have seen the columns, we shall now use list comprehension to return the third column:

print([row[2] for row in my_list])
# [3, 6, 9]

As you can see, the third column has been extracted. With that, we have demonstrated how to access or retrieve elements inside a 2D list in Python with three examples. I do hope you found this tutorial helpful!


Video, Further Resources & Summary

Do you need more explanations on how to extract elements in 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 extract elements in 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 extract elements in 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.