How to reverse an array in NumPy

You can reverse an array in the NumPy with the following code. If you want to learn Python, I highly recommend reading This Book.

How to reverse an array in NumPy
How to reverse an array in NumPy

Method 1

import numpy as np

a = np.array([1, 12, 3, 4, 15])
b = a[::-1]

print(b)

Output

[15  4  3 12  1]

Method 2

import numpy as np

a = np.array([1, 12, 3, 4, 15])
b = np.flipud(a)

print(b)

Output

[15  4  3 12  1]

Leave a Comment

Your email address will not be published.