How to get a shape of a list in Python? A list in Python doesn’t have a specific shape or size, as it can be dynamically changed by adding or removing elements. However, you can determine the length of a list using the built-in len() function. You can use the shape of a list to refer the number of elements in each dimension of the list. If a list is one-dimensional, then its shape is simply the number of elements in the list. If a list is multi-dimensional, its shape is a tuple of the number of elements in each dimension.
You can get a list shape in Python using many ways, for example, by using len(), and numpy.shape() methods. In this article, I will explain how to get the shape of the list by using all these methods with examples.
1. Quick Examples of Getting List Shape
If you are in a hurry, below are some quick examples of getting list shape.
# Quick examples of getting list shape
# Example 1: Get list shape in Python
mylist = [2, 4, 6, 8]
result = len(mylist)
# Example 2: Get the shape of a list
# Using len() function
mylist = [[2, 4, 6], [1, 3, 5], [7, 8, 9]]
num_rows = len(mylist)
num_columns = len(mylist[0])
# Example 3: Get the shape of a nested list
num_rows = len(mylist)
num_columns = len(mylist[0])
for i in range(num_rows):
if len(mylist[i]) != num_columns:
raise ValueError(“Nested lists must have the same length”)
# Example 4: Get the shape of a one-dimensional list
# Using numpy.shape() method
mylist = [1, 3, 6, 9, 2]
result = np.shape(mylist)
# Example 5: Get the shape of the list
# Using numpy.shape() method
mylist = [[2, 4, 6], [1, 3, 5], [7, 8, 9]]
result = np.shape(mylist)
# Example 6: Using numpy.shape() method
arr = np.array(mylist)
shape = arr.shape
2. Get the Shape of a One-Dimensional List in Python
You can get the shape of a one-dimensional list in Python, using the len() function. This function returns the number of elements in the list, which corresponds to the size of the one-dimensional list. For example, apply this function over the given list mylist, it will return the integer(4) which is equal to the number of elements present in a list. This is the shape of the list because it is one-dimensional.
# Get shape of the list using len()
mylist = [2, 4, 6, 8]
print(“List:”, mylist)
result = len(mylist)
print(“Shape of the list is : “, result)
Yields below output.
3. Get the Shape of a 2-Dimensional Python List
You can use the len() function to get the number of rows and the length of each row(number of columns), which gives you the shape of the 2-D list. This program first gets the number of rows in the list using len(mylist). Then it gets the length of the first row using len(mylist[0]).
This function gets the shape of a list that can be extended to higher-dimensional lists as well, with each call to len() get the length of the list along a different dimension.
# Initialize the list
mylist = [[2, 4, 6], [1, 3, 5], [7, 8, 9]]
print(“Original list: “, mylist)
# Get the shape of a list
# Using len() function
num_rows = len(mylist)
num_columns = len(mylist[0])
print(“Get the shape of rows: “, num_rows)
print(“Get the shape of a columns: “, num_columns)
Yields below output.
You can also get the shape of a nested list, using the len() function. First, you can use this function to get the number of rows and columns of a given nested list. Then, you can use a for loop over each nested list and check if it has the same length as the first nested list. If any nested list has a different length, you raise a ValueError.
# Initialize the list
mylist = [[2, 4, 6], [1, 3, 5], [7, 8, 9]]
print(“Original list: “, mylist)
num_rows = len(mylist)
num_columns = len(mylist[0])
# Get the shape of a nested list
for i in range(num_rows):
if len(mylist[i]) != num_columns:
raise ValueError(“Nested lists must have the same length”)
print(“Get the shape of a rows: “, num_rows)
print(“Get the shape of a columns: “, num_columns)
Yields the same output as above.
4. Get the Shape of a List Using numpy.shape() Method
Alternatively, you can use the numpy.shape() from the Numpy module of Python to get the shape of a list. This function returns a tuple of integers representing the shape of the list. In the below example, I will provide a 2D list called mylist with 3 rows and 3 columns and then uses the numpy.shape() method to get the shape of the list as a tuple of integers.
import numpy as np
# Initialize the list
mylist = [[2, 4, 6], [1, 3, 5], [7, 8, 9]]
print(“Original list: “, mylist)
# Get the shape of list
# Using numpy.shape() method
result = np.shape(mylist)
print(“Shape of list is : “, result)
# Output:
# Original list: [[2, 4, 6], [1, 3, 5], [7, 8, 9]]
# Shape of list is : (3, 3)
Following the below example, first, you can convert the list into a NumPy array using the np.array() function. Then you can use the shape the attribute of the NumPy array to get the shape of the array as a tuple of integers. Finally, you can access the tuple’s elements to get the list’s number of rows and columns.
# Using numpy.shape() method
arr = np.array(mylist)
shape = arr.shape
print(“Shape of list is : “, shape)
Yields the same output as above.
5. Conclusion
In this article, I have explained how to get the shape of the list of one-dimensional and two-dimensional in Python by using len(), and numpy.shape() methods with examples. And also we have learned the shape of a 1-D list is the length of the list and the shape of a 2-D list is the number of rows and columns of the list.
Happy Learning !!
How to get a shape of a list in Python? A list in Python doesn’t have a specific shape or size, as it can be dynamically changed by adding or removing elements. However, you can determine the length of a list using the built-in len() function. You can use the shape of a list to Read More Python, Python Tutorial, Pending review by admin, Python List Examples