Array Manipulation in Python

Last Updated : 14 Feb, 2026

Array manipulation involves reorganising and transforming data stored in arrays to make it suitable for analysis and computation. Operations such as reshaping, flattening, splitting and merging help adjust the structure of arrays while preserving the underlying data making them essential for efficient numerical and data processing workflows.

Flattening

Flattening converts a multi-dimensional NumPy array into a one-dimensional array while keeping all elements in the same order. It is used when a single linear sequence of data is required. NumPy provides the flatten() method for this purpose.

  • np.array(...) creates a 2D array stored in a.
  • a.flatten() converts the 2D array into a 1D array.
  • f contains all elements of a in row-wise order.
Python
import numpy as np

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

f = a.flatten()
print(f)

Output
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[1 2 3 4 5 6 7 8 9]

Reshaping

Reshaping changes the structure of an array without altering its data. The reshape() method reorganizes elements into a new shape as long as the total number of elements remains the same.

  • a.shape returns the original shape (2, 3).
  • a.reshape(3,2) reorganizes elements into 3 rows and 2 columns.
  • r.shape confirms the new shape (3, 2).
  • The values remain the same, only their arrangement changes.
Python
import numpy as np

a = np.array([[1,2,3],[4,5,6]])
print(a)
print(a.shape)

r = a.reshape(3,2)
print(r)
print(r.shape)

Output
[[1 2 3]
 [4 5 6]]
(2, 3)
[[1 2]
 [3 4]
 [5 6]]
(3, 2)

Splitting

Splitting divides a NumPy array into multiple smaller sub-arrays along rows or columns. This is useful when data needs to be processed in parts or separated logically. NumPy provides vsplit(), hsplit() and split() methods for this purpose.

1. Vertical Split: It divides an array row-wise into equal sub-arrays using the np.vsplit() method.

  • np.array(...) creates a 2D array stored in a.
  • np.vsplit(a, 4) splits a into 4 row-wise parts.
  • for x in ... prints each resulting sub-array separately.
Python
import numpy as np

a = np.array([[1,2,3,2],
              [4,5,6,2],
              [7,8,9,2],
              [7,8,9,2]])
print(a)
print('-'*10)

for x in np.vsplit(a, 4):
    print(x)

Output
[[1 2 3 2]
 [4 5 6 2]
 [7 8 9 2]
 [7 8 9 2]]
----------
[[1 2 3 2]]
[[4 5 6 2]]
[[7 8 9 2]]
[[7 8 9 2]]

2. Horizontal Split: It divides an array column-wise using the np.hsplit() method.

  • np.hsplit(a, 4) splits the array into 4 column-wise parts.
  • Each x contains one column from the original array.
  • The number of columns must be divisible by the split value.
Python
import numpy as np

a = np.array([[1,2,3,2],
              [4,5,6,2],
              [7,8,9,2],
              [7,8,9,2]])
print(a)
print('-'*10)

for x in np.hsplit(a, 4):
    print(x)

Output
[[1 2 3 2]
 [4 5 6 2]
 [7 8 9 2]
 [7 8 9 2]]
----------
[[1]
 [4]
 [7]
 [7]]
[[2]
 [5]
 [8]
 [8]]
[[3]
 [6]
 [9]
 [9]]
[[2]
 [2]
 [2]
 [2]]

3. Generic Split: The np.split() method divides an array along a specified axis at given positions.

  • axis=1 specifies column-wise splitting.
  • np.split(a, 2, axis=1) divides columns into two equal parts.
  • The result is a list containing the split sub-arrays.
Python
import numpy as np

a = np.array([[1,2,3,2],
              [4,5,6,2],
              [7,8,9,2],
              [7,8,9,2]])
print(a)
print('-'*10)

s = np.split(a, 2, axis=1)
print(s)

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

Merging

Merging refers to combining multiple NumPy arrays into a single array. NumPy provides several methods for merging such as concatenate(), hstack(), vstack() and stack(), each differing in how arrays are joined based on axes and dimensions.

1. concatenate(): The concatenate() method joins arrays along an existing axis.

  • np.array() creates one-dimensional NumPy arrays
  • np.concatenate() merges all arrays along the same axis
  • res stores the merged result as a single 1D array
Python
import numpy as np

a = np.array([1, 2, 3, 4])
b = np.array([1, 5, 2, 9])
c = np.array([9, 2, 3, 5])
d = np.array([8, 2, 9, 4])

res = np.concatenate((a, b, c, d))
print(res)

Output
[1 2 3 4 1 5 2 9 9 2 3 5 8 2 9 4]

2. hstack(): The hstack() method horizontally stacks arrays, which for 1D arrays behaves like concatenation.

  • np.hstack() stacks arrays side by side
  • For 1D arrays, it produces a single long array
  • res holds the horizontally merged output
Python
import numpy as np

a = np.array([1, 2, 3, 4])
b = np.array([1, 5, 2, 9])
c = np.array([9, 2, 3, 5])
d = np.array([8, 2, 9, 4])

res = np.hstack((a, b, c, d))
print(res)

Output
[1 2 3 4 1 5 2 9 9 2 3 5 8 2 9 4]

3. vstack(): The vstack() method vertically stacks arrays row-wise to form a 2D array.

  • np.vstack() stacks arrays vertically
  • Each input array becomes a separate row
  • res is a 2D array with 4 rows and 4 columns
Python
import numpy as np

a = np.array([1, 2, 3, 4])
b = np.array([1, 5, 2, 9])
c = np.array([9, 2, 3, 5])
d = np.array([8, 2, 9, 4])

res = np.vstack((a, b, c, d))
print(res)

Output
[[1 2 3 4]
 [1 5 2 9]
 [9 2 3 5]
 [8 2 9 4]]

4. stack(): The stack() method joins arrays along a new axis, increasing the array’s dimension.

  • np.stack() combines arrays along a new axis
  • axis=0 stacks arrays row-wise
  • The result is a higher-dimensional array compared to the input
Python
import numpy as np

a = np.array([1, 2, 3, 4])
b = np.array([1, 5, 2, 9])
c = np.array([9, 2, 3, 5])
d = np.array([8, 2, 9, 4])

res = np.stack((a, b, c, d), axis=0)
print(res)

Output
[[1 2 3 4]
 [1 5 2 9]
 [9 2 3 5]
 [8 2 9 4]]
Comment
Article Tags:

Explore