How to Reverse an Array In Python [Flip Array]

This article will discuss how to reverse an array in Python. Interestingly, there is no in-built array data structure in Python. Instead, it comes with lists that offer more robust data storage features and operations.

However, we can use the conventional arrays in Python using either array or NumPy libraries. So, our tutorial is divided into three parts.

Also ReadHow to Check If Dict Has Key in Python [5 Methods]

How to Reverse an Array In Python [Flip Array]

how to reverse an array in python

Part 1: Reversing a list in Python

Part 2: Reversing an array of Array modules in Python

Part 3: Reversing a NumPy array

Let’s now see how we can reverse arrays or lists in Python using all the methods available.

So there are different scenarios and methods to reverse an array in Python.

Also ReadHow to Convert Binary to Decimal in Python [5 Methods]

Part 1: Reverse a List in Python

In this scenario, we will discuss how to reverse a list in Python, and there are several methods to reverse a list in Python.

A list in Python is a data structure in Python used to store elements with multiple data types. We can create a list by using [].

Method 1. Using list slicing to reverse a list or array in Python

Slicing is used to get the element. We can perform slicing through the “:” operator.

The syntax will be [start_index:end_index]

Where,

start_index is the starting element, and end_index is the ending element. We can get the elements from the specified index range. The last element index is -1. So we can iterate from -1 to the beginning.

Therefore, you can reverse an array by using [:: -1].

Syntax:

List_input[::-1]

Steps:

1. Create a list

2. Apply Slicing on the list to reverse the list

3. Display the reversed list.

Code:

In this example, we create a list with 10 integer elements and reverse the list using this method.

#create a list
my_list=[23,45,32,45,43,56,66,32,89,90,55]

#display actual list
print("Actual List",my_list)

#reverse the list
reversed_list=my_list[::-1]

#display the reversed list
print("Reversed List",reversed_list)

Output:

Actual List [23, 45, 32, 45, 43, 56, 66, 32, 89, 90, 55]
Reversed List [55, 90, 89, 32, 66, 56, 43, 45, 32, 45, 23]

Also Read10 Best iPad Mini Keyboards [2022]

Method 2. Using the reverse() function to reverse a list or array in Python

By using the reverse() function, we can directly reverse a list in Python. The Python List reverse() function reverses the sorting order of items.

Syntax:

List_input.reverse()

Steps:

1. Create a list

2. Apply the reverse() function on the list

3. Display the reversed list.

Code:

We will create a list with 10 integer elements and reverse it using the reverse() function.

Code:

#create a list
my_list=[23,45,32,45,43,56,66,32,89,90,55]

#display actual list
print("Actual List",my_list)

#reverse the list
my_list.reverse()

#display the reversed list
print("Reversed List",my_list)

Output:

Actual List [23, 45, 32, 45, 43, 56, 66, 32, 89, 90, 55]
Reversed List [55, 90, 89, 32, 66, 56, 43, 45, 32, 45, 23]

Also Read10 Best Websites To Draw UML Class Diagrams Online [Free]

Method 3. Using reversed() function to reverse an array in Python

Python reversed() function returns a reversed iterator object. By using the reversed() function, we can directly reverse a list in Python.

To return a list, we have to specify a list() that surrounds the reversed() method. So by this, we will place the reversed elements into a list

Syntax:

list(reversed(List_input))

Steps:

1. Create a list

2. Apply reversed() function on the list

3. Surround this through the list() function

4. Display the reversed list.

Code:

In this example, we create a list with 10 integer elements and reverse the list using this method.

#create a list
my_list=[23,45,32,45,43,56,66,32,89,90,55]

#display actual list
print("Actual List",my_list)

#reverse the list
reversed_list=list(reversed(my_list))

#display the reversed list
print("Reversed List",reversed_list)

Output:

Actual List [23, 45, 32, 45, 43, 56, 66, 32, 89, 90, 55]
Reversed List [55, 90, 89, 32, 66, 56, 43, 45, 32, 45, 23]

Also Read2 Best Animated PNG Compressor Websites [APNG Compressors]

Part 2: Reverse an array using the array Python module

In this scenario, we will discuss how to reverse an array in Python through the array module, and there are several methods to reverse an array in Python.

An array is a one-dimensional data structure used to hold elements of similar data types. When using an array, we have to import the array module.

Syntax:

import array

Creation:

We can create an array through the array module and specify the data type of elements.

For example, to have an integer element, we have to specify as I or i.

To have a long type, we have to specify as L or l, etc.

Here, we will create integer elements, so we have to place i or I.

Syntax:

array.array('i',[elements])

Also ReadLearn Python Online With These 12 Best Free Websites

Method 1. Using reverse() function

Using the reverse() function, we can directly reverse an array in Python.

Syntax:

Array_input.reverse()

Steps:

1. Create an array.

2. Apply the reverse() function on an array.

3. Display the reversed array.

Code:

This example will create an array with 10 integer elements and reverse the array using this method.

#importing array module
import array

#create an array
my_array=array.array('i',[23,45,32,45,43,56,66,32,89,90,55])

#display actual list
print("Actual array",my_array)

#reverse the array
my_array.reverse()

#display the reversed array
print("Reversed array",my_array)

Output:

Actual array array('i', [23, 45, 32, 45, 43, 56, 66, 32, 89, 90, 55])
Reversed array array('i', [55, 90, 89, 32, 66, 56, 43, 45, 32, 45, 23])

Also ReadPython Program To Display Fibonacci Series Up To N Terms

Method 2. Using reversed() function

By using the reversed() function, we can directly reverse the array in Python.

Syntax:

array.array('i',reversed(Array_input))

Steps:

1.      Create an array

2.      Apply reversed() function on an array

3.      Display the reversed array.

Code:

In this example, we create an array with 10 integer elements and reverse the array using this method.

#importing array module
import array

#create an array
my_array=array.array('i',[23,45,32,45,43,56,66,32,89,90,55])

#display actual list
print("Actual array",my_array)

#reverse the array
result_array=array.array('i',reversed(my_array))

#display the reversed array
print("Reversed array",result_array)

Output:

Actual array array('i', [23, 45, 32, 45, 43, 56, 66, 32, 89, 90, 55])
Reversed array array('i', [55, 90, 89, 32, 66, 56, 43, 45, 32, 45, 23])

Also ReadPython Program To Display Multiplication Table of Any Number

Part 3: Reverse a NumPy array in Python

In this part, we will see how to reverse a NumPy array in Python.

NumPy stands for numeric Python used to process the arrays. We can create an array in NumPy and reverse the array using some inbuilt functions available in NumPy.

Syntax:

import numpy

Creation:

We can create an array through the NumPy module.

Syntax:

numpy.array([elements])

Also ReadHow To Automate Google Search With Python

Method 1. Using the flip() function to reverse an Array in Python

In Python, you can use the NumPy flip() function to reverse an array along a given axis. Here, the shape of the array is preserved, but the items are reordered.

This will reverse the order of the given NumPy array.

Syntax:

numpy.flip(Array_input)

Steps:

1. Create an array

2. Apply the flip() function on an array

3. Display the reversed array.

Code:

This example will create an array with 10 integer elements and reverse the array using this method.

#importing numpy module
import numpy

#create an array
my_array=numpy.array([23,45,32,45,43,56,66,32,89,90,55])

#display actual numpy
print("Actual array",my_array)

#reverse the array
result_array=numpy.flip(my_array)

#display the reversed array
print("Reversed array",result_array)

Output:

Actual array [23 45 32 45 43 56 66 32 89 90 55]
Reversed array [55 90 89 32 66 56 43 45 32 45 23]

Also Read20 Best Python Development Companies [2022]

Method 2. Using flipud() to reverse an Array in Python.

You can use the Python flipud() function to flip an array (items in each column) in an up-down direction. This function also preserves the shape of the array.

Syntax:

numpy.flipud(Array_input)

Steps:

1.      Create an array

2.      Apply the flipud() function on an array

3.      Display the reversed array.

Code:

This example will create an array with 10 integer elements and reverse the array using this method.

#importing numpy module
import numpy

#create an array
my_array=numpy.array([23,45,32,45,43,56,66,32,89,90,55])

#display actual numpy
print("Actual array",my_array)

#reverse the array
result_array=numpy.flipud(my_array)

#display the reversed array
print("Reversed array",result_array)

Output:

Actual array [23 45 32 45 43 56 66 32 89 90 55]
Reversed array [55 90 89 32 66 56 43 45 32 45 23]

Also Read20 Best Python Development Companies [2022]

Method 3. Using Slicing to reverse a NumPy Array in Python

Slicing is used to get the element. We can perform slicing through the “:” operator.

The syntax will be [start_index:end_index]

Where,

start_index is the starting element, and end_index is the ending element. We can get the elements from the specified index range. The last element index is -1. So we can iterate from -1 to the beginning.

So reversing can be made by using [:: -1].

Syntax:

List_input[::-1]

Steps:

1.      Create a NumPy array

2.      Apply Slicing on an array to reverse the NumPy array

3.      Display the reversed array.

Code:

We create a list with 10 integer elements in this example and reverse the array using this method.

#importing numpy module
import numpy

#create an array
my_array=numpy.array([23,45,32,45,43,56,66,32,89,90,55])

#display actual numpy
print("Actual array",my_array)

#reverse the array
result_array=my_array[::-1]

#display the reversed array
print("Reversed array",result_array)

Output:

Actual array [23 45 32 45 43 56 66 32 89 90 55]
Reversed array [55 90 89 32 66 56 43 45 32 45 23]

Also Read10 Best Udemy Python Courses for Beginners

Wrapping Up

So by considering these scenarios, we can reverse an array in Python through different approaches. If you still have any doubt, connect with us at [email protected], and we will try our best to assist you. Don’t forget to subscribe to our bi-weekly newsletter to deliver such valuable updates straight to your mailbox.

Scroll to Top