What are the Different Ways to Reverse a NumPy Array?

Learn via video courses
Topics Covered

Sometimes while programming in Python, we need to use Python to invert a NumPy array. In Python, the reverse of a NumPy array indicates changing the order. The first element will become the last element, the second last, the last the first, and so on. There are several methods for reversing a numpy array.

Some of the most frequent methods for reversing a numpy array are as follows:

  • Utilizing the len() with Loops
  • Using the list slicing technique
  • Using the flip() function
  • Using the reverse() function
  • Using the flipud() function
  • Using the fliplr() function

Ready? Let's work together to invert some NumPy arrays!

Using the len() with Loops

To reverse a list, we may use the len() method also known as the two-pointers approach. A developer coming from another programming language may use the following technique. This is a custom approach, not a built-in one. We'll begin by pointing to the first entry on our NumPy array. Start with an index variable pointing to zero. We will now proceed to the last entry of our NumPy array, the end index, which we will compute using the len() method. As a result, these functionalities are dependent on the user's expertise and how they built the custom code.

Consider the following example:

Code:

Output:

Using the List Slicing Technique

Let's look at another method for reversing a one-dimensional array using slicing. The slicing functionality is an expansion of the square brackets feature. This slicing allows us to get to the specific bits we need. We know the array slicing syntax is "array name[Start, Stop, Step]". Start and Stop values are optional, with the default values set to 0 for Start and the array length for Stop. If you enter step "-1", it means to reverse the array by starting at the end and finishing at the beginning.

Consider the following scenario:

Code:

Output:

We generated an array with the name arr in the preceding code. After that, we used the slicing notation on the array (arr) to get the results in reverse order. As you may expect from the result, slicing does not destroy the original list.

Slicing a NumPy array is slower than in-place approaches (like the reverse() function; It is explained in detail in the next to next section) because it creates a shallow duplicate of all entries and requires adequate memory to finish the procedure.

Using the flip() Function

The syntax of the numpy.flip() function is as follows.

Syntax:

It is made up of a few parameters.

Sr. No.Parameter NameParameter Description
1arrinput array
2axisThe default, axis=None, flips all of the input array's axes.

The numpy.flip() method in NumPy lets you flip or reverse the contents of an array along an axis. When calling numpy.flip(), indicate the array to be reversed as well as the axis. If you don't provide an axis, NumPy will reverse the contents of your input array along all of its axes.

Note :
Because we're dealing with multidimensional arrays, reversing an array may be done in various ways. In a 2d array, reversing may be done both row and column-wise.

What is the axis in input ?

The axis input is used to decide whether reversing should be done row or column wise. The orientations along the rows and columns are represented by NumPy axes, like coordinate systems. A NumPy array's beginning axis is axis 0. This axis 0, which runs vertically downward through the rows of NumPy multidimensional arrays, enables column-wise operations. Axis 1 is the second axis of multidimensional NumPy arrays. As a consequence, Axis 1 spans the array columns horizontally. It completes tasks row by row.

With this, we feel you have a good knowledge of how axes may be used to reverse a NumPy multidimensional array.

As illustrated in the following example, this approach does not destroy the original numpy array :

Code:

Output :

Using the reverse() Function

The reverse() function is a built-in Python technique that allows us to immediately reverse a list. The reverse() function always returns the sequence's reversed iterator. The biggest disadvantage is that it will function on the original list, which means it will be reversed.

The syntax of the built-in reverse() function is as follows.

Syntax :

The reverse method accepts no arguments.

Let's look at an example of how to use the reverse() method to create a reverse NumPy array.

Code :

Output:

Using the flipud() Function

The flipud() method will also reverse the Numpy array items. The numpy.flipud() method flips the array (elements in each column) up and down, keeping the shape.

The flipud() method's syntax is as follows.

Syntax :

This technique may also be used to reverse a NumPy array, as seen below:

Code :

Output :

In the preceding code, we loaded the NumPy library and then used the method numpy.array() to build a NumPy array. We made a variable and assigned it the method np.flipud(), which took the array we constructed before and displayed the output. The array was shown in reverse order as a result. We also printed the original array to ensure that it either exists or is destroyed by the flipud() function. However, we discovered that flipud() did not affect the original NumPy array.

Using the fliplr() Function

We may quickly reverse an array using the numpy.fliplr() method. The np.fliplr() function flips the array from left to right. The numpy.fliplr() method always takes an array as an argument and returns the same array with a left-right flip. Assuming you have a matrix, a request occurs requiring you to flip the items in each row while keeping the column intact. This function will come in helpful in this circumstance, and it can be accomplished with only one line of code. Moving on, we'll take a look at its syntax.

Syntax :

Let's look at an example of how to use the fliplr() method to create a reverse NumPy array.

Code :

Output :

A simple example of NumPy fliplr is shown above. In the above example, we first loaded the NumPy module. After that, we defined a two-dimensional array. We printed the result of the numpy.fliplr() method, which validates our input and function. We obtain [23,1], which is the inverse of [1,23][1,23], and [6,34][6,34], which is the inverse of [34,6][34,6]. The identical row items are flipped here while the array's form is retained. We also printed the original array to confirm that it was not destroyed by the fliplr() method. We noticed, however, that fliplr() did not affect the original NumPy array.

Conclusion

We discovered various methods for reversing a NumPy array in Python. We mostly concentrate on built-in methods because custom methods are dependent on the user's knowledge capabilities. If you're wondering what the " optimal" technique is in Python to reverse a NumPy array, my answer is "it depends".

This blog taught us:

  • For an array to be flipped, use the len() method.
  • To invert an array, use the list slicing method.
  • numpy.flip() can also be used to flip an array.
  • You may flip an array using the reverse() method.
  • The flipud() method may be used to flip an array.
  • To reverse an array, use the fliplr() method.