Difference between flip() and fliplr() functions in NumPy

Learn about the difference between flip() and fliplr() functions in Python NumPy.
By Pranit Sharma Last updated : December 25, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

Python numpy.flip() Vs numpy.fliplr() functions

Difference overview

The numpy.flip() function reverses the order of elements in an array along the given axis, whereas, the numpy.fliplr() function reverses the order of elements along axis 1 (left/right).

numpy.flip() function

In NumPy, the numpy.flip() function is used to reverse the order of elements in an array along the given axis. Here, the shape of the array is preserved, but the elements are reordered. It takes a specified axis or axes as a parameter along which to flip over the order of elements.

Below is the syntax of numpy.flip() function:

numpy.flip(m, axis=None)

numpy.fliplr() function

On the other hand, numpy.fliplr() function is used to reverse the order of elements along axis 1 (left/right). Here, it is fixed that the elements will be reversed along axis 1.For a 2-D array, this flips the entries in each row in the left/right direction. Columns are preserved but appear in a different order than before.

Below is the syntax of numpy.fliplr() function:

numpy.fliplr(m)

Let us understand with the help of an example,

Python code to demonstrate the difference between flip() and fliplr() functions in NumPy

# Import numpy
import numpy as np

# Creating a numpy array
arr = np.arange(8).reshape((2,2,2))

# Display original array
print("Original Array:\n",arr,"\n")

# using flip
res = np.flip(arr, 0)

# Display result
print("flip Result:\n",res,"\n")

# using fliplr
res = np.fliplr(arr)

# Display result
print("fliplr Result:\n",res)

Output

Example: Difference between flip() and fliplr() functions in NumPy

Web References

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.