AttributeError: list object has no attribute shape ( Solved )

AttributeError_ list object has no attribute shape ( Solved )

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.

AttributeError: list object has no attribute [ Attribute_Name] ( Solved )

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.

AttributeError list object has no attribute shape root cause
AttributeError list object has no attribute shape root cause

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
list object has no attribute shape root cause
list object has no attribute shape root cause

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))
list object has no attribute shape fix
list object has no attribute shape fix

Conclusion –

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.

AttributeError list object has no attribute split ( Solved )

AttributeError list object has no attribute split ( Solved )

Thanks

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner