Get Size of 2D List in Python (3 Examples)
Hi! This tutorial will demonstrate how to find the dimensions of a 2D list in the Python programming language.
Here is a quick overview:
Let’s get into the Python code!
Create Demo 2D List
We will create the demo 2D list of integers whose proportions we will get 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]] print(my_list) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(type(my_list)) # <class 'list'>
With the 2D list of integers created, we can now demonstrate different ways to get its dimensions.
Example 1: Find Dimensions of 2D List Using len() Function
In this first example, we are going to use Python’s len() function to get the measurements of the 2D list:
# Get the number of rows num_rows = len(my_list) # Get the number of columns num_cols = len(my_list) print("Number of rows:", num_rows) print("Number of columns:", num_cols) # Number of rows: 3 # Number of columns: 3
In the above example, we used the
len() function to get the number of rows in the 2D list, which is essentially the number of sublists inside the 2D list.
We also used the
len() function to get the number of columns, which is basically the length of individual sublists inside the 2D list.
Example 2: Find Dimensions of 2D List Using While Loop
In this second example, we will use a while loop to find the proportions of the 2D list:
num_rows = 0 while num_rows < len(my_list): num_rows += 1 num_cols = 0 while num_cols < len(my_list): num_cols += 1 print("Number of rows:", num_rows) print("Number of columns:", num_cols) # Number of rows: 3 # Number of columns: 3
The while loops above essentially count the number of rows and columns in the 2D list by incrementing
num_cols by 1, both having started at 0.
The increment continues for as long as their values are less than the length of the 2D list (the number of rows) and the length of individual sublists (the number of columns) in the 2D list, respectively.
Example 3: Find Dimensions of 2D List Using NumPy
For this example, we will need to install and import Python’s NumPy library:
# install NumPy pip install numpy # import NumPy import numpy as np
With NumPy installed and imported into our Python environment, let us now demonstrate how to get the shape of the 2D list:
num_rows, num_cols = np.shape(my_list) print("Number of rows:", num_rows) print("Number of columns:", num_cols) # Number of rows: 3 # Number of columns: 3
The np.shape() function returns the dimensions of a 2D array or list, i.e. its number of rows and columns. Therefore, parsing the 2D list, “my_list”, to the function returns the size of the list. Python’s unique syntax makes it possible to assign values to variables by separating them with a comma in one line, as demonstrated above.
With that, we have demonstrated three easy ways to get the size or dimensions of a 2D list in Python. I do hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to get the size of 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 get the size of a 2D list in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to checkout other interesting Python list tutorials on Statistics Globe, starting with these:
- count() Method for Lists in Python (2 Examples)
- List Comprehension in Python (Example)
- Difference between List & Set in Python
- Difference between List & pandas DataFrame in Python
- Learn Python Programming
This post has shown how to get the size of 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