


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]
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)
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)
Output
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.
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()
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)
Output
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.
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)
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)
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().
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)
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)
Output
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Notepad++ to run a Python program requires the following steps: 1. Install the Python plug-in; 2. Create a Python file; 3. Set the run options; 4. Run the program.

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

Llama3 is here! Just now, Meta’s official website was updated and the official announced Llama 38 billion and 70 billion parameter versions. And it is an open source SOTA after its launch: Meta official data shows that the Llama38B and 70B versions surpass all opponents in their respective parameter scales. The 8B model outperforms Gemma7B and Mistral7BInstruct on many benchmarks such as MMLU, GPQA, and HumanEval. The 70B model has surpassed the popular closed-source fried chicken Claude3Sonnet, and has gone back and forth with Google's GeminiPro1.5. As soon as the Huggingface link came out, the open source community became excited again. The sharp-eyed blind students also discovered immediately

The Python program development process includes the following steps: Requirements analysis: clarify business needs and project goals. Design: Determine architecture and data structures, draw flowcharts or use design patterns. Writing code: Program in Python, following coding conventions and documentation comments. Testing: Writing unit and integration tests, conducting manual testing. Review and Refactor: Review code to find flaws and improve readability. Deploy: Deploy the code to the target environment. Maintenance: Fix bugs, improve functionality, and monitor updates.

Flask installation and configuration tutorial: A tool to easily build Python Web applications, specific code examples are required. Introduction: With the increasing popularity of Python, Web development has become one of the necessary skills for Python programmers. To carry out web development in Python, we need to choose a suitable web framework. Among the many Python Web frameworks, Flask is a simple, easy-to-use and flexible framework that is favored by developers. This article will introduce the installation of Flask framework,

What is GIL? GIL is the abbreviation of global interpreter lock, which is an important concept of python interpreter. The GIL ensures that the Python interpreter can only execute one thread at a time. This means that at any time, only one thread can run Python bytecode. Other threads must wait for the GIL to be available before continuing execution. How does GIL work? The GIL is a lock written in C and located in the Python interpreter. When a thread wants to execute Python bytecode, it must first obtain the GIL. If the GIL is already held by another thread, that thread must wait for the GIL to be available before continuing execution. What impact does the GIL have on Python programs? GIL for Python
