How to reverse an array in python?

An array is a collection of a group of elements stored at a contiguous memory location. The data items of an array are of the same data type and are ordered. An array can be handled in python by importing an array or NumPy module.

A list in python can be used instead of an array but a list has no constraint on the data type of items. This article discusses different methods to reverse an array of the array module and NumPy array in python.

Reverse an array of the array module

Python does not support built-in array data structures. An array module can create an array-like object. The array module takes two arguments, type code, and initializer to declare an array.

Type code is a single character( 'i' represents signed int, 'd' represents double, etc). that represents the data type of items. An initializer is an optional argument and takes a list as a parameter.

The syntax for declaring an array from the array module is as follows.

#syntax:
import array
arr = array.array(typecode , initializer)

The different methods to reverse an array of array module are given below.

Reverse an array by using the reverse() method

This reverse() method directly modifies the original array. The reverse() method doesn't take any arguments and doesn't return any value. The syntax for the reverse() method is as follows.

#syntax:
arr.reverse()

For example, let us consider an array arr of integer data type initialized to [1, 2, 3, 4, 5]. The arr.reverse() method reverses the items of array arr.

import array
arr = array.array('i', [1, 2, 3, 4, 5])
print("Original array: ", arr)
arr.reverse()
print("Reversed array : ", arr)

The output for the reversed array

Original array:  array('i', [1, 2, 3, 4, 5])
Reversed array :  array('i', [5, 4, 3, 2, 1])
Reverse an array by using the reversed() method

reversed() method takes a single argument that can be an array variable. The reversed() method returns an iterator in reverse order. The main difference between the reverse() and reversed() method is reversed() function doesn't modify the original array.

The syntax for the reversed() method is as follows.

#syntax:
 reversed(arr)

Consider an array arr of integer data type initialized to [1, 2, 3, 4, 5]. The reversed(arr) reverses the array and returns the output as [5, 4, 3, 2, 1].

import array
arr = array.array('i', [1, 2, 3, 4, 5])
print("Original array: ", arr)
rev_array = array.array('i', reversed(arr))
print("Reversed array : ", rev_array)

Output

Original array:  array('i', [1, 2, 3, 4, 5])
Reversed array :  array('i', [5, 4, 3, 2, 1])

Reverse a NumPy array

NumPy is a Numerical Python library in python that contains array and matrix data structures. NumPy module have different functions to perform mathematical operations on arrays. The array object in NumPy is called ndarray.

NumPy provides array objects that are much faster than traditional array lists in the context of time. The syntax to declare an array in NumPy is as follows.

#syntax: 
import numpy
my_arr = numpy.array(list)​

The different methods to reverse a NumPy array are given below.

Reverse an array by using flip()

The flip() method reverses the array in the given axis. flip() takes two arguments, array and axis(optional), and returns an array object after reversing the array.

For example, let us consider an array arr of integer data type [2, 4, 6, 8, 10]. numpy.flip(arr) returns a reversed array as output.

import numpy
arr = numpy.array([2, 4, 6, 8, 10])
print("Original array: ", arr)
rev_arr =  numpy.flip(arr)
print("Reversed array: ", rev_arr)

Output

Original array:  [ 2  4  6  8 10]
Reversed array:  [10  8  6  4  2]
Reverse an array by using flipud()

The flipud() method reverses an array in an up/down direction. It takes a single argument that can be an array variable and returns a reverse array object. The syntax for the flipud() method is as follows.

#syntax:
import numpy
numpy.flipud(array)

Let us consider, an array arr of integer data type [2, 4, 6, 8, 10]. numpy.flipud(arr) returns a reversed array as output.

import numpy
arr = numpy.array([2, 4, 6, 8, 10])
print("Original array: ", arr)
rev_arr =  numpy.flipud(arr)
print("Reversed array: ", rev_arr)

Output

Original array:  [ 2  4  6  8 10]
Reversed array:  [10  8  6  4  2]
Reverse an array by using extended slicing

The syntax for an extended slice is [start: stop: step]. The value for the start and stop field is optional and if the value is not provided it sets the default value for start to '0' and stop to string length. Step -1 indicates starting from the end and stop at the start hence, reversing the array.

Let us consider, an array arr of integer data type [2, 4, 6, 8, 10]. arr[::-1] returns a reversed array as output.

import numpy
arr = numpy.array([2, 4, 6, 8, 10])
print("Original array: ", arr)
rev_arr =  arr[::-1]
print("Reversed array: ", rev_arr)

Output

Original array:  [ 2  4  6  8 10]
Reversed array:  [10  8  6  4  2]
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions