# How to change array size in Python

To change the size of an array in Python, use the reshape method of the numpy library.

**reshape(a,d)**

- The first parameter (a) is the name of the array (vector or matrix) to be transformed.
- The second parameter (d) is a number or a tuple indicating the number of rows and columns of the new array.

The **reshape** function changes the size of the array without deleting the elements.

**Note**. The new size must equal the cardinality of the old array. For example, if a vector has 10 elements, it can be transformed into a 5x2 or 2x5 matrix. It cannot be made into a 3x3 matrix or anything else.

## Example

__Example 1 (vector to matrix)
__

Create an array of 10 elements using the **array** method.

import numpy as np

x=np.array([1,2,3,4,5,6,7,8,9,10])

The new array x has one size. It is a vector.

Change the array to a 5x2 array using the function **reshape**.

y=np.reshape(x,[5,2])

The new array y has two dimensions.

>>> y

array([[ 1, 2],

[ 3, 4],

[ 5, 6],

[ 7, 8],

[ 9, 10]])

It has the same elements as the vector x but arranged in a matrix.

__Example 2__

To get the same result, use reshape as the method.

y=x.reshape([5,2])

The end result is the same.

__Example 3 (from matrix to vector)__

Create a 2x5 matrix

import numpy as np

x=np.array([[1,2,3,4,5],[6,7,8,9,10]])

The array x has two dimensions

>>> x

array([[ 1, 2, 3, 4, 5],

[ 6, 7, 8, 9, 10]])

Transform the matrix into a vector.

z=np.reshape(x,10)

The new array has one size.

>>> z

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

**Report an error or share a suggestion to enhance this page**