How to Reverse an Array in Python?

Recently in a project for USA clients, I got a scenario to reverse an array in Python, that deals with the states of USA. In this tutorial, We will explore various methods to achieve this, including built-in functions, slicing, and custom implementations.

What are Python Arrays?

Before we get into reversing arrays, let’s quickly recap what Python arrays are, lists represent arrays. Lists are data structures that can store elements of different data types. They are ordered, mutable, and allow duplicate values.

For example, let’s say we have a list of U.S. states:

states = ["California", "Texas", "Florida", "New York", "Illinois"]

Check out How to Initialize an Array in Python

Reverse an Array in Python

Let us see different ways to reverse an array in Python:

1. Reverse a Python Array using the reverse() Method

The simplest way to reverse an array in Python is by using the built-in reverse() method. This method modifies the original array in place, reversing the order of its elements. Here’s an example:

states = ["California", "Texas", "Florida", "New York", "Illinois"]
states.reverse()
print(states)

Output:

['Illinois', 'New York', 'Florida', 'Texas', 'California']

I executed the above example code, you can see the output in the screenshot below:

Reverse an Array in Python

As you can see, the reverse() method reversed the order of the elements in the states array.

Read How to Multiply an Array by a Scalar in Python

2. Reverse a Python Array using Slicing

Another way to reverse an array in Python is by using slicing. Slicing allows you to create a new array with the elements in reverse order without modifying the original array. Here’s how you can use slicing to reverse an array:

states = ["California", "Texas", "Florida", "New York", "Illinois"]
reversed_states = states[::-1]
print(reversed_states)

Output:

['Illinois', 'New York', 'Florida', 'Texas', 'California']

I executed the above example code, you can see the output in the screenshot below:

How to Reverse an Array in Python

In this example, we use the slicing syntax [::-1] to create a new array reversed_states with the elements states in reverse order. The original states array remains unmodified.

Check out How to Find the Closest Value in an Array Using Python

Slicing Syntax

Slicing provides an efficient way to reverse an array without modifying the original array. Let’s break down the slicing syntax [::-1]:

  • The first colon (:) separates the start and end indices of the slice.
  • The second colon (:) separates the end index and the step value.
  • The -1 step value indicates that we want to traverse the array from right to left, effectively reversing the order.

Read How to Remove Duplicates from a Sorted Array in Python

3. Reverse a Python Array using Iteration

If you want more control over the reversal process or need to perform additional operations while reversing the array, you can use iteration. Here’s an example of reversing an array using a for loop:

states = ["California", "Texas", "Florida", "New York", "Illinois"]
reversed_states = []

for state in states[::-1]:
    reversed_states.append(state)

print(reversed_states)

Output:

['Illinois', 'New York', 'Florida', 'Texas', 'California']

I executed the above example code, you can see the output in the screenshot below:

How to Reverse an Array in Python using Iteration

In this approach, we create an empty array reversed_states to store the reversed elements. We then iterate over the states array in reverse order using slicing [::-1] and append each element to the reversed_states array.

Check out How to Iterate Through a 2D Array in Python

4. Reverse a Python Array using the reversed() Function

Python provides a built-in reversed() function that returns an iterator object that yields the elements of the array in reverse order. Here’s an example:

states = ["California", "Texas", "Florida", "New York", "Illinois"]
reversed_states = list(reversed(states))
print(reversed_states)

Output:

['Illinois', 'New York', 'Florida', 'Texas', 'California']

In this example, we pass the states array to the reversed() function, which returns an iterator. We then convert the iterator to a list using the list() function to obtain the reversed array.

Read How to Distinguish Between Arrays and Lists in Python

Reverse NumPy Array in Python

If you’re working with NumPy arrays, which are optimized for numerical computations, you can use slicing to reverse the array efficiently. Here’s an example:

import numpy as np

temperatures = np.array([72.5, 68.2, 75.8, 69.1, 73.4])
reversed_temperatures = temperatures[::-1]
print(reversed_temperatures)

Output:

[73.4, 69.1, 75.8, 68.2, 72.5]

In this example, we create a NumPy array temperatures representing temperatures in Fahrenheit. We then used slicing [::-1] to create a new array reversed_temperatures with the elements in reverse order.

Check out How to Append to an Array in Python

Conclusion

In this tutorial, we explored various methods to reverse an array in Python. I helped you to learn how to use the reverse() method to modify the array in place, slicing to create a new reversed array, iteration for more control, the reversed() function to obtain an iterator, and slicing with NumPy arrays.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.