In this tutorial, we’ll learn how to create and use Python 2D list. We’ll be using proper examples with step by step explanation in order for better understanding.
- Introduction: Python 2D List
- Implementing Python 2D List (Easy Examples)
- Creating a Python 2D List
- Fetch Data From 2D List
- Replace Data In 2D List
- Display All 2D List Data Using Python Nested For Loop
Introduction: Python 2D List
Python 2D list is a very powerful and useful. In this concept, we’ve a Python list and the items of that list are also lists. It looks like a matrix. Matrix is a concept in math which is like a rectangular array.
Let’s now understand Python 2D practically. We’ll first create a 2D list in Python. After that, we’ll understand how to fetch some specific data from it. Also, we’ll see how to replace the existing data with new one.
Implementing Python 2D List (Easy Examples)
See below guide in which we’ll create, fetch and replace data in 2D Python list.
Creating a Python 2D List
For that, we’ve to create a Python list. For demonstration, we’ll specify three lists in it. See below code:
twoDList=[ [1,2,3], [5,6,7], [9,10,11] ]
We now have a 2D list. Let’s now see how to fetch value of a specific item from that list.
Fetch Data From 2D List
[1, 2, 3]
We can fetch data from list using the square brackets. We just have to enter the specific index of that item which we want to fetch value of. In the above code, we’ve specified the index 0 which shows the output [1,2,3] as its the first item of the list. This item is also a list but its taken as a single item here.
Let’s now see how to fetch the number 3 from that list. See below code:
Using double brackets, we can fetch the value of nested list. As its in 3rd position so we’ve used index 2 as index starts from 0.
Let’s now fetch the number 10 from our Python 2D list. See below code:
Now we know how to fetch data from 2D list. Let’s now see how to replace data of a specific item within list.
Replace Data In 2D List
For that, we’ve to use assignment operator(=). It’s really simple. We’ve to point to the specific index in 2D list using the method specified above. After that, we’ve to use the assignment operator to assign a new value to it. See below example:
print(twoDList) # 5 twoDList=23 # value is set to 23 print(twoDList) # 23
5 # initial value 23 # replaced value
Let’s now see our 2D list as well. See below:
print( twoDList )
[ [1, 2, 3], [23, 6, 7], [9, 10, 11] ]
We can see that the value has been successfully replaced. So this is how we can replace data inside 2D list as well.
Display All 2D List Data Using Python Nested For Loop
twoDList=[ [1,2,3], [5,6,7], [9,10,11,12] ] for val in twoDList: for value in val: print(value)
1 2 3 5 6 7 9 10 11 12
First for loop will get first item of list which is itself a list. Then second for loop will fetch each data of first item(which is a nested list). After that, first for loop will take the second item and second for loop will fetch and display all the items of second list and so on.
So this is how we can properly create and use Python 2D list. Feel free to share it with other Python developers.
Don’t hesitate to ask if you still have any questions regarding 2D list in Python. We’ll be very happy to answer all.
To conclude this post, hope you now have a detailed practical knowledge of how to create and use Python 2D list. You’re highly encouraged to give your feedback on this post.
Do visit our other posts on Python programming as well. Thank you for reading this article.