HOWTO · NumPy

How to Reverse a NumPy Array in Python

Reverse a NumPy array in Python with basic slicing, numpy.flipud(), or numpy.flip(), and choose the method by view and axis behavior.

On this page

To reverse a NumPy array in Python, use basic slicing with [::-1], numpy.flipud(), or numpy.flip(). Slicing is concise for reversing a one-dimensional array and returns a view. numpy.flipud() reverses rows along axis 0, while numpy.flip() can reverse a chosen axis or all axes. The examples below show how their results differ.

Reverse a NumPy Array With the Basic Slicing Method in Python

For a one-dimensional array, array[::-1] selects elements from end to start with a step of -1. Run this example:

import numpy as np

array = np.array([1, 2, 3, 4, 5])
reverse = array[::-1]
print(reverse)
[5 4 3 2 1]

The slice is a view that refers to the same data as array; it does not make an independent copy. Therefore, assigning through reverse also changes the corresponding element in array. If the original values must remain independent, make a copy with array[::-1].copy(). This approach is useful when the goal is a compact one-dimensional reversal and view behavior is acceptable. For a multidimensional array, the same expression reverses only its first axis; use an axis-aware function when you need to specify which dimension changes.

Reverse a NumPy Array With the numpy.flipud() Function in Python

numpy.flipud() reverses an array along axis 0. For a one-dimensional array, axis 0 is its only axis, so the values appear in reverse order. For a two-dimensional array, axis 0 is the row direction: the rows change order, but the values within each row keep their order. The NumPy numpy.flipud() documentation describes this function.

import numpy as np

array = np.array([[1, 2], [3, 4]])
print(np.flipud(array))
[[3 4]
 [1 2]]

Choose numpy.flipud() when reversing axis 0 is the intended operation, especially for matrices where that means reversing row order. For a one-dimensional array it is a readable alternative to slicing; for another axis, use numpy.flip() and name that axis explicitly.

Reverse a NumPy Array With the numpy.flip() Function in Python

numpy.flip() reverses the entries along the axis you pass. With a two-dimensional array, axis=1 reverses the columns within each row. If axis is omitted, its default is None, which reverses every axis. That changes both row order and the order within each row; it does not flatten the array into one sequence. See the NumPy numpy.flip() documentation.

import numpy as np

array = np.array([[1, 2], [3, 4]])
print(np.flip(array, axis=1))
print(np.flip(array))
[[2 1]
 [4 3]]
[[4 3]
 [2 1]]

Use numpy.flip(array, axis=0) for row reversal, axis=1 for column reversal in a two-dimensional array, or omit axis to reverse all dimensions. For a one-dimensional array, omitting axis reverses its only axis. In practice, choose slicing for a short one-dimensional expression, numpy.flipud() when axis 0 is specifically required, and numpy.flip() when you need to state an axis or reverse every axis. These operations change element order along dimensions; they do not sort values or reshape the array.