Reshaping Arrays

The same data can be arranged into different shapes without copying or recalculating anything — a flat list of 6 numbers can become a 2x3 grid, a 3x2 grid, or back to flat again, as long as the total element count matches.

reshape and the shape attribute

>>> flat to grid and back
import numpy as np

flat = np.arange(1, 7)
grid = flat.reshape(2, 3)
print(grid)
print(grid.shape)
print(grid.flatten())
Output
[[1 2 3]
 [4 5 6]]
(2, 3)
[1 2 3 4 5 6]

flat.reshape(2, 3) rearranges the same 6 numbers into 2 rows of 3 columns. .shape confirms the array's current dimensions as a tuple. .flatten() reverses the process, collapsing any array back down to one dimension.

Note: reshape requires the new shape to hold exactly as many elements as the original — flat.reshape(2, 4) on a 6-element array raises ValueError: cannot reshape array of size 6 into shape (2,4). Also, reshape usually returns a view that shares memory with the original array where possible, not a copy — changing a value in the reshaped array can change the original too. Use .reshape(...).copy() if you need an independent array.