- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to reverse the elements of the array using inbuilt function
An array is a data structure that is used to store homogeneous elements in order. And the stored elements are identified by an index value or a key. Python doesn’t have a specific data structure to represent arrays. However, we can use the List data structure or Numpy module to work with arrays.
In the article below, we will see how to reverse the elements of an array using the python built-in functions. Reverse the elements of an array means changing the order of array elements from front to back.
Input Output Scenarios
Let us now look at some input output scenarios to understand the reversing of the array elements.
Input array: [3, 5, 1, 4, 0, 2] Output array: [2, 0, 4, 1, 5, 3]
The order or arrangement of input array elements are reversed.
Using the inbuilt function reversed()
By using the Python built-in reversed() function we can reverse the elements of an array. Following is the syntax –
reversed(seq)
The function takes an iterator as only parameter and it returns a reversed iterator. The function returns a list_reverse iterator object, so we need to use the list function to get the reversed list.
Example
Let’s take an example and reverse the elements of an array using the reversed() function.
# creating array arr = [5, 2, 1, 6, 8] print ("The original array is: ", arr) print() # Reverse the elements of the array result = list(reversed(arr)) print("The array after reversing the elements is:", result)
Output
The original array is: [5, 2, 1, 6, 8] The array after reversing the elements is: [8, 6, 1, 2, 5]
The reversed() function changed the order of the array elements.
Using the list.reverse() function
The list.reverse() in Python is used to reverse the elements of a list object. Following is the syntax of this function –
list_obj.reverse()
The reverse() method does not take any parameter and it does not return any output instead it will update the original list object.
Example
In this example, we will use the list.reverse() function.
# creating array arr = [5, 2, 1, 6, 8] print ("The original array is: ", arr) print() # Reverse the elements of the array arr.reverse() print("The array after reversing the elements is:", arr)
Output
The original array is: [5, 2, 1, 6, 8] The array after reversing the elements is: [8, 6, 1, 2, 5]
The revers() method updated the given list arr with reversed elements.
Using the numpy.flip() function
We can use the numpy built-in function flip() to reverse the elements of an array. The numpy.flip() function returns a new numpy array with reversed array elements, and it does not change the original array. Following is the syntax –
numpy.flip(m, axis=None)
Where,
m − Input array
axis − It is an optional parameter, it takes an integer or tuple of integers by default it is None.
Example
In this example, initially we will define a numpy array object and then we will use numpy.flip() function to reverse the array elements.
import numpy as np # creating array arr = np.array([9, 3, 2, 1, 6, 8, 5]) print("The original array is: ", arr) print() # Reverse the elements of the array result = np.flip(arr) print("The array after reversing the elements is:", result)
Output
The original array is: [9 3 2 1 6 8 5] The array after reversing the elements is: [5 8 6 1 2 3 9]
We successfully reversed the array elements using the numpy built-in function flip().
Using numpy.flipud() method
We can use the numpy built-in function flipud() to reverse the elements of numpy array along the 0th axis. The function is equivalent to array[::-1]. Followinbg is the syntax to do so –
numpy.flipud(m)
The parameter m represents an input array whose elements has to be reversed.
Example
In this example, we will reverse the array elements using the numpy.flipud() function.
import numpy as np # creating array arr = np.array([9, 3, 1, 6, 8]) print("The original array is: ", arr) print() # Reverse the elements of the array result = np.flipud(arr) print("The array after reversing the elements is:", result)
Output
The original array is: [9 3 1 6 8] The array after reversing the elements is: [8 6 1 3 9]
These are the few built-in functions in python which are used to reverse the array elements.