Create Empty 2D List in Python (2 Examples)
Hi! This short tutorial will show you how to make an empty 2D list in the Python programming language. For demonstration, we will be building an empty 2D list of None values.
Here is a quick overview:
Let’s jump into the Python code!
Example 1: Make Empty 2D List Using Nested For Loop
In this example, we will make use of a nested for loop to build the empty 2D list. Run the code block below in your preferred Python IDE:
rows = 3 cols = 4 my_list =  for i in range(rows): row =  for j in range(cols): row.append(None) my_list.append(row) print(my_list) # [[None, None, None, None], [None, None, None, None], [None, None, None, None]] print(type(my_list)) # <class 'list'>
In the above example, we, first of all, initialized an empty 2D list with 3 rows and 4 columns. Then, we used the first loop to create rows that corresponded to the number of rows we initialized using the range() function.
With the second loop, we created columns of “None” values, which corresponded to the number of columns we initialized, and appended that to the rows using the append() method. The rows were then appended to the empty 2D list called “my_list”.
Example 2: Make Empty 2D List Using List Comprehension
Here, we will make use of list comprehension method to generate an empty 2D list in Python:
rows = 3 cols = 4 my_list = [[None for j in range(cols)] for i in range(rows)] print(my_list) # [[None, None, None, None], [None, None, None, None], [None, None, None, None]] print(type(my_list)) # <class 'list'>
Like the previous example, we initialized 3 rows and 4 columns for the empty 2D list. Then, with the list comprehension method, we ran iterations that created 3 rows and 4 columns that were populated with the “None” value and were then stored inside of the empty 2D list called “my_list”.
As seen, the list comprehension method works just like the nested for loop in the previous example, only a more concise approach. With that, we have demonstrated how to create an empty 2D list in Python. I do hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to create an empty 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 create an empty 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)
- Convert List to Matrix & Vice-Versa in Python (Examples)
- Learn Python Programming
- Convert List from Boolean to String in Python (2 Examples)
This post has shown how to create an empty 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