*In Python, the list data structure stores elements in sequential order. The numpy.shape() function gives us the number of elements in each dimension of an array. We cannot use the shape function on a list. If we try to use the numpy.shape() function on a list, you will raise the error “AttributeError: ‘list’ object has no attribute ‘shape’”.*

*We have to convert the list to a numpy array using numpy.array() before trying to use any NumPy functions. We can check what the type of an object is by using type()*

*This tutorial will go into detail on the error definition. We will go through an example that causes the error and how to solve it.*

## Table of contents

## AttributeError: ‘list’ object has no attribute ‘shape’

*AttributeError* occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “*‘list’ object has no attribute ‘shape’*” tells us that the list object we are handling does not have the shape attribute. We will raise this error if we try to call the numpy.shape() method on a list object. shape() is a NumPy function that returns a tuple containing the number of elements in each dimension of an array.

### NumPy shape Syntax

The syntax for the NumPy array method shape is as follows:

`numpy.shape(a)`

**Parameters**:

- a: Input array

**Returns:**

- shape: tuple of ints. The elements of the shape tuple provide the lengths of the input array dimensions.

Let’s look at an example of getting the shape of two NumPy arrays:

```
import numpy as np
arr = np.array([[2, 4, 6], [1, 3, 5]])
arr2 = np.array([[[2, 4], [6, 8]],[[1, 3], [5, 7]]])
print(f'The shape of the first array is {arr.shape}')
print(f'The shape of the second array is {arr2.shape}')
```

```
The shape of the first array is (2, 3)
The shape of the second array is (2, 2, 2)
```

The example above tells us that the shape of the first array is (2, 3) and the second array is (2, 2, 2). arr has two dimensions and each dimension has three elements. arr2 has three dimensions and each dimension has two rows and two columns.

## Example

Let’s look at an example of where we try to get the shape of a list:

```
lst = [[2, 4, 6], [8, 10, 12]]
print(lst.shape)
```

```
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
1 lst = [2, 4, 6, 8, 10, 12]
2
----≻ 3 print(lst.shape)
AttributeError: 'list' object has no attribute 'shape'
```

We get an error because we can only get the shape of NumPy arrays.

### Solution: Convert List to NumPy Array Using numpy.array()

To convert a list to an array we can use the numpy.array() method. Let’s look at the revised code:

```
lst = [[2, 4, 6], [8, 10, 12]]
print(type(lst))
arr = np.array(lst)
print(type(arr))
print(f'The shape of the array is {arr.shape}')
```

Let’s run the code to get the result:

```
≺class 'list'≻
≺class 'numpy.ndarray'≻
The shape of the array is (2, 3)
```

The output tells us that the original object is a list, the numpy.array() method returns a NumPy ndarray, and that the shape of this array is (2, 3). The array has two dimensions and each dimension has three elements.

## Summary

Congratulations on reading to the end of this tutorial! The error “AttributeError: ‘list’ object has no attribute ‘shape’” occurs when you try to use the NumPy array method shape to get the shape of a list.

The shape() method is suitable for NumPy arrays. If you want to use the shape() method you have to convert the list to an array using the numpy.array() method.

Generally, check the type of object you are using before you call the shape() method.

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.