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.
How to check the Attributes of the list 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.
AttributeError: list object has no attribute [ Attribute_Name] ( Solution ) –
There are two types of generic solutions for this type of error. Let’s explore these-
1. Solution 1: Changing the object as per attribute –
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.
2 . Solution 2: Changing the Attribute as per object –
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.
AttributeError: list object has no attribute ( Similar Errors )-
There are often multiple errors related to attributes in the class like :
- ‘list’ object has no attribute ‘split’
- ‘list’ object has no attribute ‘items’
- ‘list’ object has no attribute lower
- ‘list’ object has no attribute replace
- ‘list’ object has no attribute head
- ‘list’ object has no attribute shape
- ‘list’ object has no attribute strip
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.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.