22.4 C
New York
Wednesday, December 4, 2024
HomeProgrammingPythonHow to Reverse an Array In Python

How to Reverse an Array In Python [Flip Array]

In this tutorial, you will learn how to reverse an array in Python using lists, array module, and NumPy package. Learn with examples here.

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.

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 available methods.

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. Several methods are available.

A list in Python is a data structure 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 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]

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

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 the reversed() function to reverse an array in Python

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

We must specify a list() surrounding the reversed() method to return a list. 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]

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. Several methods are available to reverse an array in Python.

An array is a one-dimensional data structure that holds elements of similar data types. To use an array, we must 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 must specify as I or i.

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

We will create integer elements here, so we must 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])

Method 2. Using reversed() function

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])

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, which is 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

Python’s NumPy flip() function can 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]

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]

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]

Wrapping Up

So, by considering these scenarios, we can reverse an array in Python through different approaches. If you still have any doubts, contact us at himanshu@codeitbro.com, and we will try our best to assist you.

Don’t forget to subscribe to our bi-weekly newsletter to deliver valuable updates straight to your mailbox.

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES