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
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'>
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:
print(my_list) # [4, 5, 6]
Python indexing starts at 0; therefore, in the example above,
my_list 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:
print(my_list) print(my_list) print(my_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,
[1, 2, 3], which is the first element in the 2D list. To return the second element in the sublist, we write
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: print(i) # [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 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:
- Convert List of Tuples to List of Lists in Python (3 Examples)
- count() Method for Lists in Python (2 Examples)
- Learn Python Programming
- Convert List of Matrix to List of Lists & Vice-Versa in Python (Examples)
- Difference between List & Set in Python
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.
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.
Statistics Globe Newsletter