Python Reverse NumPy Array
Maybe your like
While I was working on a data analysis project, I needed to reverse a NumPy array for time-series visualization. The issue is, there are multiple ways to reverse arrays in NumPy, each with different performance implications and use cases.
In this tutorial, I will cover five simple methods you can use to reverse NumPy arrays in Python (from using built-in functions to manual approaches).
So let’s dive in!
Table of Contents
- Reverse NumPy Arrays in Python
- Method 1 – Use NumPy’s flip() Function
- Method 2 – Use Array Slicing with ::-1
- Method 3 – Use flipud() and fliplr() Functions
- Method 4 – Use the reverse() Function with tolist()
- Method 5 – Use np.sort() with [::-1]
- Performance Comparison
- Real-world Application: Time Series Visualization
Reverse NumPy Arrays in Python
Let me show you some important methods to reverse a NumPy array in Python.
Read NumPy Array to List in Python
Method 1 – Use NumPy’s flip() Function
The simplest way to reverse a NumPy array is by using the np.flip() function. This Python built-in method allows you to reverse an array along any specified axis.
Here’s how to reverse a 1D array:
import numpy as np # Create a sample array arr = np.array([1, 2, 3, 4, 5]) # Reverse the array reversed_arr = np.flip(arr) print(reversed_arr)Output:
[5 4 3 2 1]I executed the above example code and added the screenshot below.

For multi-dimensional arrays, you can specify the axis along which to reverse:
# Create a 2D array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Reverse along rows (axis=0) reversed_rows = np.flip(arr_2d, axis=0) print("Reversed rows:") print(reversed_rows) # Reverse along columns (axis=1) reversed_cols = np.flip(arr_2d, axis=1) print("Reversed columns:") print(reversed_cols) # Reverse both dimensions reversed_both = np.flip(arr_2d) print("Reversed both:") print(reversed_both)The flip() function is memory-efficient as it returns a view of the input array with reversed indices, not a new copy (unless the input is a non-contiguous array).
Check out np.savetxt() Function in Python
Method 2 – Use Array Slicing with ::-1
Another common method is to use Python’s slicing with a negative step. This is a concise and readable way to reverse arrays:
import numpy as np # Create a sample array arr = np.array([1, 2, 3, 4, 5]) # Reverse using slicing reversed_arr = arr[::-1] print(reversed_arr)Output:
[5 4 3 2 1]I executed the above example code and added the screenshot below.

For multi-dimensional arrays, you can specify which axis to reverse:
# Create a 2D array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Reverse rows reversed_rows = arr_2d[::-1, :] print("Reversed rows:") print(reversed_rows) # Reverse columns reversed_cols = arr_2d[:, ::-1] print("Reversed columns:") print(reversed_cols) # Reverse both reversed_both = arr_2d[::-1, ::-1] print("Reversed both:") print(reversed_both)I often prefer this method for its simplicity and readability, especially for 1D arrays or when I need to explain the code to others.
Read np.genfromtxt() Function in Python
Method 3 – Use flipud() and fliplr() Functions
Python NumPy provides two specialized functions for 2D arrays: flipud() (flip up/down) and fliplr() (flip left/right).
import numpy as np # Create a 2D array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Flip up/down (reverse rows) flipped_ud = np.flipud(arr_2d) print("Flipped up/down:") print(flipped_ud) # Flip left/right (reverse columns) flipped_lr = np.fliplr(arr_2d) print("Flipped left/right:") print(flipped_lr)Output:
Flipped up/down: [[7 8 9] [4 5 6] [1 2 3]] Flipped left/right: [[3 2 1] [6 5 4] [9 8 7]]I executed the above example code and added the screenshot below.

These functions are particularly useful when working with image data or matrices where the directional language (up/down, left/right) makes more intuitive sense than axis numbers.
Check out NumPy Reset Index of an Array in Python
Method 4 – Use the reverse() Function with tolist()
If you need to work with Python lists temporarily, you can convert the NumPy array to a list, reverse it, and then convert it back:
import numpy as np # Create a sample array arr = np.array([1, 2, 3, 4, 5]) # Convert to list, reverse, and convert back arr_list = arr.tolist() arr_list.reverse() reversed_arr = np.array(arr_list) print(reversed_arr) # Output: [5 4 3 2 1]This approach works well for 1D arrays but requires additional handling for multi-dimensional arrays. It’s also less efficient than the previous methods due to the conversions between NumPy arrays and Python lists.
Read Create a Python Empty Matrix
Method 5 – Use np.sort() with [::-1]
For sorted arrays, you can combine sorting with slicing to reverse the order:
import numpy as np # Create a sorted array arr = np.array([1, 2, 3, 4, 5]) # Sort in descending order reversed_arr = np.sort(arr)[::-1] print(reversed_arr) # Output: [5 4 3 2 1]This method is particularly useful when you want to sort an array in descending order:
# Create an unsorted array unsorted = np.array([3, 1, 5, 2, 4]) # Sort in descending order desc_sorted = np.sort(unsorted)[::-1] print(desc_sorted) # Output: [5 4 3 2 1]Check out Random Number Between Two Values in Numpy
Performance Comparison
When working with large datasets, performance matters. Here’s a quick comparison of the methods using a large array:
import numpy as np import time # Create a large array large_arr = np.arange(1000000) # Method 1: np.flip() start = time.time() result1 = np.flip(large_arr) print(f"np.flip(): {time.time() - start:.6f} seconds") # Method 2: Slicing with ::-1 start = time.time() result2 = large_arr[::-1] print(f"Slicing: {time.time() - start:.6f} seconds") # Method 4: tolist() and reverse() start = time.time() arr_list = large_arr.tolist() arr_list.reverse() result4 = np.array(arr_list) print(f"tolist() and reverse(): {time.time() - start:.6f} seconds")In my tests, np.flip() and slicing with ::-1 are consistently the fastest methods, while the list conversion approach is significantly slower.
Read Create a Matrix in Python
Real-world Application: Time Series Visualization
Let’s look at a practical example where reversing arrays is useful. Imagine we have stock price data for Apple Inc. over several months, and we want to visualize it from the most recent to the oldest:
import numpy as np import matplotlib.pyplot as plt # Sample data: Apple stock prices over 10 months (simplified) months = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct']) prices = np.array([145.85, 121.73, 116.36, 131.24, 124.61, 133.98, 146.15, 151.83, 141.50, 149.80]) # Reverse both arrays to show most recent data first reversed_months = np.flip(months) reversed_prices = np.flip(prices) # Plot the data plt.figure(figsize=(10, 6)) plt.plot(reversed_months, reversed_prices, marker='o', linestyle='-', color='b') plt.title('Apple Stock Price (Most Recent First)') plt.xlabel('Month') plt.ylabel('Price ($)') plt.grid(True) plt.tight_layout() plt.show()This visualization presents the data in reverse chronological order, which can be more intuitive for financial analysis, where recent trends are often the most relevant.
I hope you found this article helpful. In this article, I have explained five methods to reverse a numpy array: using NumPy’s flip() function, using array slicing with ::-1, using flipud() and fliplr() functions, using the reverse() function with tolist(), and using np.sort() with [::-1]. I also discussed performance comparison and real-world applications.
You may also like to read:
- Python NumPy Matrix Operations
- 0-Dimensional Array NumPy in Python
- NumPy Shape in Python
Bijay KumarI am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
enjoysharepoint.com/Tag » How To Reverse An Array Python
-
Reverse An Array In Python - 10 Examples - AskPython
-
How Do I Reverse A List Or Loop Over It Backwards? - Stack Overflow
-
Reversing A List In Python - GeeksforGeeks
-
Python | Reverse A Numpy Array - GeeksforGeeks
-
How To Use Python To Reverse A List Or Array - MakeUseOf
-
Python Reverse List – Reversing An Array In Python - FreeCodeCamp
-
Python Reverse List – How To Reverse A Range Or Array
-
Python Program To Print The Elements Of An Array In Reverse Order
-
5 Methods To Reverse Array In Python (reverse, Recursion Etc)
-
Reverse The Array Using Python - PrepInsta
-
How To Reverse An Array In Python [Flip Array] - CodeItBro
-
NumPy: Reverse An Array - W3resource
-
Python List Reverse() - Programiz
-
Reverse An Array In Python Code Example - Code Grepper