AppDividend
Latest Code Tutorials

np.fliplr: What is Numpy fliplr() Function

The Numpy.fliplr() function flips the entries in each row in the left/right direction. The columns are preserved but appear in a different order than before.

np.fliplr

The np.fliplr() is a numpy library function that flips the array(entries in each column) in left-right direction. The numpy flipr() function accepts an array as an argument and returns the array the same array as flipped in the left-right direction. The shape of an array is preserved.

Syntax

numpy.fliplr(array)

Parameters

The fliplr() function takes an array as a parameter.

Return Value

The fliplr() function returns the array the same array as flipped in the left-right direction.

Programs on fliplr() method in Python

Write the program to show the fliplr() function’s working in Python.

import numpy as np

arr = np.arange(4).reshape((2, 2))
print("Original array : \n", arr)
print("\nFlipped array left-right : \n", np.fliplr(arr))

Output

Original array :
 [[0 1]
 [2 3]]

Flipped array left-right :
 [[1 0]
 [3 2]]

In this example, we can see that by taking a 2×2 array, we flipped the element from the same row from the left-right rotation.

Write a program to take a 3×3 array and flip elements using fliplr() and print the output.

import numpy as np

arr = np.arange(9).reshape((3, 3))
print("Original array : \n", arr)
print("\nFlipped array left-right : \n", np.fliplr(arr))

Output

Original array :
 [[0 1 2]
 [3 4 5]
 [6 7 8]]

Flipped array left-right :
 [[2 1 0]
 [5 4 3]
 [8 7 6]]

In this example, we can see that we are getting an array with elements being flipped row-wise by taking a 3×3 matrix and using the fliplr() function.

See also

Numpy all()

Numpy any()

Numpy around()

Numpy flip()

Numpy flipud()

Leave A Reply

Your email address will not be published.

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