How to Reverse a 1D & 2D numpy array using np.flip() and [] operator in Python

In this article we will discuss different ways to reverse the contents of 1D and 2D numpy array ( columns & rows ) using np.flip() and [] operator.

Reverse 1D Numpy array using [] operator trick

First of all import numpy module i.e.

import numpy as np

Now suppose we have a numpy array i.e.
# Create a Numpy array from list of numbers
arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11])

Now let’s reverse the contents of the above created numpy array using a small trick,
# Get a reversed view of numpy array
reversedArr = arr[::-1]

print('Reversed Array : ', reversedArr)

Output:
Reversed Array :  [11  8  2  4  3  9 18  2  4  1  6]

How did it worked ?

In numpy array we use the [] operator with following syntax,

arr[start:end:stepsize]

It will basically select the elements from start to end with step size as stepsize. We did not provided start and end parameter, therefore by default it picked the complete array. Also, the step size was -1, therefore it selected the elements from last to first.

Reverse Array is View Only

reversedArr is just a reversed view of the arr. It means any modification in this view reversedArr will also be reflected in the original array arr i.e.

# Modification in reversed array will be reflected in original array too
reversedArr[1] = 100

print('Modified Reversed Array : ', reversedArr)
print('Original Array : ', arr)

Output:
Modified Reversed Array :  [ 11 100   2   4   3   9  18   2   4   1   6]
Original Array :  [  6   1   4   2  18   9   3   4   2 100  11]

Advertisements

Reverse Numpy array using np.flip()

Python’s Numpy module provides a function to flip the contents of numpy array along different axis i.e.

numpy.flip(arr, axis=None)

Arguments:

  • arr : Numpy array
  • axis : Axis along which it needs to flip / reverse the contents.
    • If None: flip / reverse contents along all of the axes of the input array

Let’s understand by examples,

Reverse 1D Numpy array using np.flip()

Suppose we have a numpy array i.e.

# Create a Numpy array from list of numbers
arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11])

Now let’s reverse the contents of the above created numpy array using a np.flip(),
# Reverse the contents of 1D numpy Array
reversedArr = np.flip(arr)

print('Reversed Array : ', reversedArr)

Output:
Reversed Array :  [11  8  2  4  3  9 18  2  4  1  6]

As it was 1D numpy array, so we don’t need to provide axis parameter.

Reverse 2D Numpy Array using np.flip()

Let’s create a 2D Numpy array i.e.

# Create a 2D Numpy array list of list
arr2D = np.array([[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]])

print(arr2D)

Contents of the 2D numpy array are,
[[11 12 13 11]
 [21 22 23 24]
 [31 32 33 34]]

Reverse contents in all rows and all columns of 2D Numpy Array

If we don’t provide any axis parameter in np.flip() then fit will reverse contents along all of the axes of the 2D array i.e.

# Reverse contents of each row and column in the Numpy Array
reversedArr = np.flip(arr2D)

print('Reversed Array : ')
print(reversedArr)

Output:
Reversed Array : 
[[34 33 32 31]
 [24 23 22 21]
 [11 13 12 11]]

It flipped contents in all rows and all columns of 2D Numpy Array

Reverse contents of all rows only in 2D Numpy Array

# Reverse contents of each column in the Numpy Array
reversedArr = np.flip(arr2D, axis=0)

print('Reversed Array : ')
print(reversedArr)

Output:
Reversed Array : 
[[31 32 33 34]
 [21 22 23 24]
 [11 12 13 11]]

Reverse contents of all columns only in 2D Numpy Array
# Reverse contents of each row in the Numpy Array
reversedArr = np.flip(arr2D, axis=1)

print('Reversed Array : ')
print(reversedArr)

Output:
Reversed Array : 
[[11 13 12 11]
 [24 23 22 21]
 [34 33 32 31]]

Reverse contents of only one column in 2D Numpy Array

# Reverse contents of 2nd column i.e. column at index position 1 in 2D Numpy Array
arr2D[:,1] = np.flip(arr2D[:,1])

print('Reversed Array : ')
print(arr2D)

Output:
Reversed Array : 
[[11 32 13 11]
 [24 23 22 21]
 [31 12 33 34]]

It flipped the contents of 2nd column only in numpy array.

Reverse contents of only one row in 2D Numpy Array

# Reverse contents of 2nd row i.e. row at index position 1 in 2D Numpy Array
arr2D[1] = np.flip(arr2D[1])

print('Reversed Array : ')
print(arr2D)

Output:
Reversed Array : 
[[11 12 13 11]
 [24 23 22 21]
 [31 32 33 34]]

It flipped the contents of 2nd row only in numpy array.

Complete example is as follows :

import numpy as np

def main():

  print('**** Reverse a Numpy array using operator [] ****')

  # Create a Numpy array from list of numbers
  arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11])

  print('Original Array : ', arr)

  print('*** Reverse a Numpy Array ***')

  # Get a reversed view of numpy array
  reversedArr = arr[::-1]

  print('Reversed Array : ', reversedArr)

  # Modification in reversed array will be reflected in original array too
  reversedArr[1] = 100

  print('Modified Reversed Array : ', reversedArr)
  print('Original Array : ', arr)


  print('**** Reverse a Numpy array using np.flip() ****')

  # Create a Numpy array from list of numbers
  arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11])

  print('Original Array : ', arr)

  # Reverse the contents of 1D numpy Array
  reversedArr = np.flip(arr)

  print('Reversed Array : ', reversedArr)

  print('***** Reverse a 2D Numpy Array *****')

  # Create a 2D Numpy array list of list
  arr2D = np.array([[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]])

  print('2D Numpy Array')
  print(arr2D)

  print('** Reverse all rows and all columns of the Numpy Array **')

  # Reverse contents of each row and column in the Numpy Array
  reversedArr = np.flip(arr2D)

  print('Reversed Array : ')
  print(reversedArr)

  print('** Reverse all columns only in Numpy Array **')

  # Reverse contents of each column in the Numpy Array
  reversedArr = np.flip(arr2D, axis=0)

  print('Reversed Array : ')
  print(reversedArr)

  print('** Reverse all rows only in Numpy Array **')

  # Reverse contents of each row in the Numpy Array
  reversedArr = np.flip(arr2D, axis=1)

  print('Reversed Array : ')
  print(reversedArr)

  print('** Reverse contents of 2nd rows in Numpy Array **')

  # Reverse contents of 2nd row i.e. row at index position 1 in 2D Numpy Array
  arr2D[1] = np.flip(arr2D[1])

  print('Reversed Array : ')
  print(arr2D)

  print('Reverse contents of 2nd column in Numpy Array')

  # Reverse contents of 2nd column i.e. column at index position 1 in 2D Numpy Array
  arr2D[:,1] = np.flip(arr2D[:,1])

  print('Reversed Array : ')
  print(arr2D)

if __name__ == '__main__':
  main()


Output:
**** Reverse a Numpy array using operator [] ****
Original Array :  [ 6  1  4  2 18  9  3  4  2  8 11]
*** Reverse a Numpy Array ***
Reversed Array :  [11  8  2  4  3  9 18  2  4  1  6]
Modified Reversed Array :  [ 11 100   2   4   3   9  18   2   4   1   6]
Original Array :  [  6   1   4   2  18   9   3   4   2 100  11]
**** Reverse a Numpy array using np.flip() ****
Original Array :  [ 6  1  4  2 18  9  3  4  2  8 11]
Reversed Array :  [11  8  2  4  3  9 18  2  4  1  6]
***** Reverse a 2D Numpy Array *****
2D Numpy Array
[[11 12 13 11]
 [21 22 23 24]
 [31 32 33 34]]
** Reverse all rows and all columns of the Numpy Array **
Reversed Array : 
[[34 33 32 31]
 [24 23 22 21]
 [11 13 12 11]]
** Reverse all columns only in Numpy Array **
Reversed Array : 
[[31 32 33 34]
 [21 22 23 24]
 [11 12 13 11]]
** Reverse all rows only in Numpy Array **
Reversed Array : 
[[11 13 12 11]
 [24 23 22 21]
 [34 33 32 31]]
** Reverse contents of 2nd rows in Numpy Array **
Reversed Array : 
[[11 12 13 11]
 [24 23 22 21]
 [31 32 33 34]]
Reverse contents of 2nd column in Numpy Array
Reversed Array : 
[[11 32 13 11]
 [24 23 22 21]
 [31 12 33 34]]

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top