AttributeError: list object has no attribute shape error occurs when we invoke shape attribute from list type object. However, shape() is not defined within the list class. The best way to fix this issue is to use len() function. Since this list data structure is only one dimensional and it has only length. Hence the shape will be nothing but the length. The shape() function works with two-dimensional data structures like dataframe or multi-dimensional n-array.
AttributeError: list object has no attribute shape ( Solution ) –
There are two main concepts to fix this error. One is to either change the object or class which has the correct attributeerror. The second way is to change the attribute with the correct base class which provides the same functionality. To understand the concept read the below article.
Now, lets us specifically understand to fix this error but before that, we will replace this error, and then we will fix it. For this we will create the sample list and then invoke the shape() AttributeError.
Solution 1: Converting a list to Numpy Array –
Here we will convert the list to a numpy array and then we invoke this shape function. Since numpy has the already shape() function attribute then the interpreter will not throw any error.
import numpy sample_list=['A','B','C'] sample_arr=numpy.array(sample_list) sample_arr.shape
Solution 2 : Using len() function as alternative –
Since the intent is to check the shape and hence we can use len() function like below.
import numpy sample_list=['A','B','C'] print(len(sample_list))
There is so many errors in a similar context but the fix will be on the same pitch. Please go through the below article for a strong hold.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.