The list allows you to store more than one variable in one single variable. It is mutable and can manipulate easily using various inbuilt functions in it. If you are encountering with AttributeError: list object has no attribute shape error then this post is for you. You will learn how to easily solve this error.
The root cause of AttributeError: list object has no attribute shape Error
The root cause of why you are getting this type of error is that you are using the functions on the list that are not defined with it.
When you use the dir(list) then you will see all the functions and attribute defined by it.
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Let’s say I have a list with string variables as elements. And if I want to get the dimension of the list using the shape method then I will get the shape error.
Run the following lines of code.
list_strings = ["ABC","DEF","GHI","JKL"] list_strings.shape()
The solution of list object has no attribute shape
You are getting the error as the shape function is defined by the numpy module, not the list object. To solve this error you have to first convert the list to a numpy array and then use the shape function on it.
To convert the list to a numpy array you have to pass the list as an argument to the numpy.array().
Use the below lines of code to convert the list to a numpy array.
After that use the shape method on the converted numpy array. You will know the number of rows and columns for the numpy array.
import numpy as np list_strings = ["ABC","DEF","GHI","JKL"] array= np.array(list_strings) array.shape
In this post, you have learned how to solve the list object has no attribute shape error. Firstly you have to convert the list to numpy array and lastly apply the shape method to find the number of rows and columns in a converted list.