AttributeError : ‘list’ object has no attribute ‘reshape’ ( Solved )

'list' object has no attribute 'reshape' ( Solved )

If you are a python programmer then you must know about the list. It is a data structure that contains multiple mutable objects in a single variable. Suppose you want to change the dimension of the list then how you can do it? If you use the reshape() method then you will encounter the error AttributeError: ‘list’ object has no attribute ‘reshape’. In this tutorial, you will know how to solve this error easily.

What is reshape() method?

The reshape() method allows you to change the shape or dimensions of the existing array. You can convert the one-dimensional array to a multi-dimensional array using the same number of elements inside the array. For example, you can convert the single-dimensional numpy array to the multip-dimensional array using the numpy.reshape() function.

Why does the AttributeError: ‘list’ object has no attribute ‘reshape’ occurs?

The leading cause for getting this ‘list’ object has no attribute “reshape” error is that the list does not has the reshape() function to change the dimensions. It is the function provided by the numpy python package.

You will get the error when you will run the below lines of code.

sample_list = [100,200,300,400,500,600]
sample_list.rehsape(3,2)

Output

list object has no attribute reshape error
list object has no attribute reshape error

If you run the dir(list) then you will see all the functions that are supported by the list object. There are append,pop, clear e.t.c functions but not the reshape() method.

print(dir(list))

Output

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', 
'__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__'
, '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 
'insert', 'pop', 'remove', 'reverse', 'sort']

Solution for the ‘list’ object has no attribute ‘reshape’ Error

The solution for this attributeError is very simple. If you want to change the shape or dimensions of the array then firstly convert the list to numpy array. After that use the numpy.reshape() function to change the dimension.

Execute the below lines of code to change the shape of the array.

import numpy as np
sample_list = [100,200,300,400,500,600]
numpy_array = np.array(sample_list)
numpy_array.reshape(3,2)

Output

Changing the shape of the list using numpy
Changing the shape of the list using numpy

You can see the shape of the array has been changed to 3 rows and 2 columns.

Conclusion

The list is widely used by python programmers. If you want to change the dimensions of the list then convert it into a numpy array first as the reshape() method is a function of the numpy module. The above solution will solve the no attribute ‘reshape’ error. If you want to read more about generic solution, please read the below article.

AttributeError: list object has no attribute [ Attribute_Name] ( Solved )

I hope you have liked this tutorial. If you have any suggestions or queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner