Home >Backend Development >Python Tutorial >Python program using built-in function to reverse elements of array
An array is a data structure used to store homogeneous elements in order. Stored elements are identified by index values or keys. Python has no specific data structure to represent arrays. However, we can use List data structure or Numpy module to handle arrays.
In the following article, we will learn how to reverse the elements of an array using python built-in functions. Reversing array elements means changing the order of array elements from front to back.
Now let's look at some input and output scenarios to understand the inversion of array elements.
Input array: [3, 5, 1, 4, 0, 2] Output array: [2, 0, 4, 1, 5, 3]
The order or arrangement of the input array elements is reversed.
By using Python’s built-in reverse() function, we can reverse the elements of an array. The following is the syntax -
reversed(seq)
This function takes an iterator as the only parameter and returns a reverse iterator. This function returns a list_reverse iterator object, so we need to use the list function to get the reverse list.
Let us take an example of reversing the elements of an array using the reverse() 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)
The original array is: [5, 2, 1, 6, 8] The array after reversing the elements is: [8, 6, 1, 2, 5]
reverse() function changes the order of array elements.
list.reverse() in Python is used to reverse the elements of a list object. The following is the syntax of the function -
list_obj.reverse()
The reverse() method takes no parameters and does not return any output, but updates the original list object.
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)
The original array is: [5, 2, 1, 6, 8] The array after reversing the elements is: [8, 6, 1, 2, 5]
revers() method updates the given list arr with reversed elements.
We can use the numpy built-in function flip() to reverse the elements of the array. The numpy.flip() function returns a new numpy array with opposite array elements, and it does not change the original array. The following is the syntax -
numpy.flip(m, axis=None)
where,
m - input array
axis - It is an optional parameter, it accepts an integer or a tuple of integers, by default it is None .
In this example, initially we will define a numpy array object and then reverse the array elements using the numpy.flip() function.
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)
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().
We can flip the elements of a numpy array along the 0th axis using the numpy built-in function flipud(). This function is equivalent to array[::-1]. Here is the syntax to do this –
numpy.flipud(m)
Parameter m represents an input array whose elements must be reversed.
In this example, we will flip 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)
The original array is: [9 3 1 6 8] The array after reversing the elements is: [8 6 1 3 9]
These are several built-in functions in python for reversing array elements.
The above is the detailed content of Python program using built-in function to reverse elements of array. For more information, please follow other related articles on the PHP Chinese website!