Attributeerror: list object has no attribute shape [SOLVED]

The “AttributeError: ‘list’ object has no attribute ‘shape'” is a Python error message that occurs when you try to access the “shape” attribute of a list object.

In this article, we are going to fix the Attributeerror: list object has no attribute shape. We will provide a brief discussion, of the causes, and solutions regarding the error.

What is Attributeerror: list object has no attribute shape?

In Python, the AttributeError is a type of error that is raised when an object does not have the attribute that is being referenced or called.

In this particular case, the error message “AttributeError: list object has no attribute ‘shape'” means that you are trying to access the shape attribute of a Python list object. However, list objects do not have a shape attribute, which is commonly used with numpy arrays.

The shape attribute is used to get the dimensions of an array. Therefore, this error usually occurs when you are trying to treat a list as an array and are using a function or attribute that is specific to arrays, such as shape.

Why Attributeerror: list object has no attribute shape occurs?

The error “AttributeError: ‘list’ object has no attribute ‘shape'” occurs because the attribute “shape” is not defined for a list object in Python.

The “shape” attribute is used to retrieve the dimensions of a NumPy array, but it is not applicable to a Python list because lists are not structured as arrays with defined dimensions.

Here’s an example code that could raise the AttributeError:

import numpy as np

my_list = [1, 2, 3, 4, 5]
my_list.shape

In this code, we first import the numpy library, which provides an array object that has a shape attribute. Then, we create a simple Python list my_list with five elements.

The error occurs when we try to access the shape attribute of my_list by calling my_list.shape. Since my_list is a list and not a numpy array, it does not have a shape attribute, resulting in the AttributeError being raised.

Output

my_list.shape
AttributeError: 'list' object has no attribute 'shape'

How to Fix Attributeerror: list object has no attribute shape?

There are two ways to resolve This AttributeError. One is to modify the object or class that is causing the error to have the correct attribute. The other way is to modify the attribute itself by using a base class that provides the desired functionality. To clearly understand this concept, refer to read the article below.

Solution Number 1. Convert my_list to a numpy array using the np.array() function.

If you are trying to work with arrays, you should use NumPy arrays instead of lists.

Try to use this given code example:

my_array = np.array(my_list)
print(my_array.shape)

Here is the first solution that we apply to our given example error above.

import numpy as np

my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
print(my_array.shape)

Output

(5,)

Solution Number 2. Use functions that are specific to lists, such as len() to get the length of the list.

If you are not working with arrays and just need to access the length of a list, you can use the len() function as follows:

print(len(my_list))

Here is the Second solution that we apply to our given example error above

import numpy as np

my_list = [1, 2, 3, 4, 5]
print(len(my_list))

Output

5

Conclusion

In conclusion, the article Attributeerror: list object has no attribute shape is a Python error message that occurs when you try to access the “shape” attribute of a list object.

By following the given solution, surely you can fix the error quickly and proceed to your coding project again.

If you have any questions or suggestions, please leave a comment below. For more attributeerror tutorials in Python, visit our website.

Leave a Comment