Python program using built-in function to reverse elements of array

PHPz
Release: 2023-09-06 15:49:08
forward
1197 people have browsed it

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.

Input and output scenarios

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]
Copy after login

The order or arrangement of the input array elements is reversed.

Use the built-in function reverse()

By using Python’s built-in reverse() function, we can reverse the elements of an array. The following is the syntax -

reversed(seq) 
Copy after login

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.

Example

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)
Copy after login

Output

The original array is:  [5, 2, 1, 6, 8]
The array after reversing the elements is: [8, 6, 1, 2, 5]
Copy after login
Copy after login

reverse() function changes the order of array elements.

Use list.reverse() function

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() 
Copy after login

The reverse() method takes no parameters and does not return any output, but updates 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)
Copy after login

Output

The original array is:  [5, 2, 1, 6, 8]
The array after reversing the elements is: [8, 6, 1, 2, 5]
Copy after login
Copy after login

revers() method updates the given list arr with reversed elements.

Use numpy.flip() function

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)
Copy after login

where,

  • m - input array

  • axis - It is an optional parameter, it accepts an integer or a tuple of integers, by default it is None .

Example

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)
Copy after login

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]
Copy after login

We successfully reversed the array elements using the numpy built-in function Flip().

Use numpy.flipud() method

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)
Copy after login

Parameter m represents an input array whose elements must be reversed.

Example

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)
Copy after login

Output

The original array is: [9 3 1 6 8]
The array after reversing the elements is: [8 6 1 3 9]
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!