How to Fix AttributeError: ‘list’ object has no attribute ‘reshape’

Diagram of How to Fix AttributeError: 'list' object has no attribute 'reshape'

Diagram

AttributeError: ‘list’ object has no attribute ‘reshape’ error occurs when you try to operate the reshape() method on the list object, but it does not exist on the list. You can use the reshape() attribute on the numpy object.

Reproduce the error

main_list = [21, 11, 19, 46]

main_list.rehsape(3, 2)

Output

Why the error 'list' object has no attribute 'reshape' occurs

How to fix the error?

To fix the AttributeError: ‘list’ object has no attribute ‘reshape’ error, convert an input list to an array using numpy.array() method and then use the reshape() attribute on the numpy array object.

import numpy as np

main_list = [21, 11, 19, 46]

main_arr = np.array(main_list)

print(main_arr.reshape(2, 2))

Output

fixing the error

You can see that we get the output without errors because now we are calling the reshape() method on the numpy object, not the list object.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.