AttributeError: list object has no attribute [ Attribute_Name] error occurs when we access any attribute which is not defined in the list class of python. There are multiple attributes that create a lot of confusion like ‘shape’ or ‘len’ etc. Usually, shape() and len() functions are to check the dimensions of different data structures in python. Here len() function is defined in the list python class. Here if invoke the shape function with list type object, The interpreter will through the AttributeError. Well, In this article we will zoom in explicitly for attributeerrors related to list type of object.
If you want to check the supported function with a list object, just run the below code. It will print all the supported function and attribute with the list object.
There are two types of generic solutions for this type of error. Let’s explore these-
In this type of fix, we will change the object type which supports that attribute. To simplify this, let’s take an example. Suppose we invoke shape() function which list object. Since list does not support shape() function. We can convert the list object to dataframe Series which supports this shape() attribute and perform similar functionality.
Since the underline object is not defined in the list type of object, we will search and replace the attribute with similar functionality with the list class itself. Again let’s take an example, as shape() is not available in the list class. The intent of using the shape function is to check the dimension of the list. A similar function for this functionality in the list is len(). We will invoke this function with a list-type object to solve this AttributeError.
There are often multiple errors related to attributes in the class like :
Apart from the above most frequent error. There are multiple attributes like add, copy, drop, iteritems, keys, encode, etc which also create confusion and end up in a similar AttributeError. The Generic solution will remain the same for such similar errors. The specification will change on the basis of the module.
Thanks
Data Science Learner Team