Python Array - Reverse


Reverse Array in Python

In Python, you can reverse the order of items in a given array, using reverse() method of array instance.

In this tutorial, you will learn how to reverse a given Python array, with examples.

To reverse a Python array my_array, call reverse() method on the array object, as shown in the following code snippet.

my_array.reverse()

The reverse() method updates the original array.

1. Reverse given Integer Array

In the following program, we take an integer array my_array with some initial values, and then reverse the order of items in the array using reverse() method.

Python Program

import array as arr

my_array = arr.array('i', [10, 15, 20, 25, 30, 35])
my_array.reverse()

print(my_array)

Output

array('i', [35, 30, 25, 20, 15, 10])

2. Reverse given Character Array

In this example, we shall take a unicode character array, and reverse the items in it.

Python Program

import array as arr

my_array = arr.array('u', 'apple')
my_array.reverse()

print(my_array)

Output

array('u', 'elppa')

Summary

In this Python Array tutorial, we learned how to reverse a given Python Array, using reverse() method, with examples.