Here we will learn How to get the shape of a list in python? Now we will discuss how to get a shape of a one-dimensional list using len and numpy? How to get the shape of a two-dimensional list using len and numpy? And how to get the shape of a nested list? This article will be fascinating. At the end of an article, you will get a clear idea about getting a shape of a list. The shape of a list will be obtained using a built-in function len() and a module NumPy.
The shape of a list normally returns the number of objects in a list. We can calculate a shape of a list using two methods, len() and NumPy array shape. Numpy has an attribute named np.shape to get the shape of a list or tuple. We can also calculate a shape for the nested list.
What is len() function?
len() is a built-in function in python that is useful for calculating the given sequence’s length. The given sequence may be a list, string, tuple, array, dictionary, etc.
Syntax
len(sequence)
Parameter
sequence: list, string, tuple, array, dictionary, etc.
Returns
Length of the given sequence.
Calculating shape of a list: One-dimensional
lst = [3,6,9,12,15,18,21]
print("Shape of a list:",len(lst))
Create a list. Then using len() to calculate the shape of the list in python.
Output
Shape of a list: 7
Calculating a shape of a list: Two dimensional list
lst=[[6,4],[5,3],[2,4]]
row=len(lst)
column=len(lst[0])
print(f'Rows:{row}, Column:{column}')
print("Shape of a list:",len(lst))
Create a list. Next to calculate the row and column of the list using the len() function. Finally, calculating the shape of a list.
Output
Rows:3, Column:2 Shape of a list: 3
Get a shape of nested lists
def dimension(a,position,val):
if len(a)>position:
if a[position]<val:
a[position]=val
else:
a.append(val)
return a
lst=[[2,4,6],[5,6,7],[[3,4,5],[7,9,5],4],5,[6,7,4]]
a=[]
dimension(a,0,len(lst))
row=len(lst)
column=len(lst[0])
print(f'Rows:{row}, Column:{column}')
print("Shape of a list:",a)
Output
Rows:5, Column:3 Shape of a list: [5]
What is Numpy.shape()?
Numpy has an attribute shape that is useful to calculate the shape f array, list, dictionary, tuple, etc.
Syntax
np.shape(sequence)
Parameter
sequence: list, string, tuple, array, dictionary, etc.
Returns
the shape of the given sequence
Calculating shape of a list: One-dimensional
import numpy as np
lst=[1,2,3,4,5,6]
print("Shape of a list:",np.shape(lst))
Import numpy module. Create a list. Then using numpy.shape() to calculate the shape of the list in python.
Output
Shape of a list: (6,)
Calculating a shape of a list: Two dimensional list
import numpy as np
my_lst=[[6,4],[5,3],[2,4]]
print("Shape of a list: ",np.shape(my_lst))
Import NumPy module. Create a list. Next to calculate the row and column of the list using the len() function. Finally, calculating the shape of a list.
Output
Shape of a list: (3, 2)
Calculating a shape for nested list
import numpy as np
my_lst=[[6,4],[[5,3],[2,4]]],[[9,5],[8,6,7]]
print("Shape of a list: ",np.shape(my_lst))
Output
Shape of a list: (2, 2)
Find the shape of the list using recursion
from collections.abc import Sequence
def get_shape(lst, shapelst=()):
if not isinstance(lst, Sequence):
return shapelst
if isinstance(lst[0], Sequence):
innerlen = len(lst[0])
if not all(len(item) == innerlen for item in lst):
raise ValueError("All lists dont have same length")
shapelst += (len(lst), )
shapelst = get_shape(lst[0], shapelst)
return shapelst
print("Shape of list 1:",get_shape([1,2,3,4]))
print("Shape of list 2:",get_shape([[1,2],[1,2],[1,3],[1,4]]))
print("Shape of list 3:",get_shape([[[1,2],[1,2]],[[1,3],[1,4]]]))
Output
Shape of list 1: (4,) Shape of list 2: (4, 2) Shape of list 3: (2, 2, 2)
FAQs Related on How to Get List Shape in Python
We can calculate a shape of a list using len() function and numpy.shape() function.
Yes, we can calculate the shape of a nested list using len() and numpy.shape().
Conclusion
In this article, we have learned how to calculate a shape of a list in python? We have completely learned how to get a shape using len() and numpy.shape(). This will be very useful to the python beginner. Try to solve the programs on your own. In case of any queries, ask it in a comment section. We hope this is very useful to everyone. Learn python in python pool.