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)
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.
['__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)
You can see the shape of the array has been changed to 3 rows and 2 columns.
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.
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.