NumPy ndarray.byteswap() Method | Swap bytes of the Array Elements Last Updated : 05 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The ndarray.byteswap() method swaps the bytes of the array elements. It toggles between low-endian and big-endian data representation by returning a byteswapped array, optionally swapped in-place. Note: byteswap() method does not work on arrays of strings. Example Python3 # Python program explaining # byteswap() function import numpy as geek # a is an array of integers. a = geek.array([1, 256, 100], dtype = np.int16) print(a.byteswap(True)) Output [256 1 25600]SyntaxSyntax: ndarray.byteswap(inplace=False) Parameters inplace : [bool, optional] If True, swap bytes in-place, default is False. Returns out : [ndarray] The byteswapped array. If inplace is True, this is a view to self.How to swap the byte order of the data in the ndarrayTo toggle between low-endian and big-endian data representation we use ndarray.byteswap() method of NumPy library. This is useful when the data has been written in a machine with a different byte order than the one where it is being read. Let's understand it better with an example: Example 1: Python3 import numpy as np # Create a NumPy array of float64 type A = np.array([1.5, 2.5, 3.5], dtype=np.float64) print(A) # Output: [1.5 2.5 3.5] # Byteswap the array A.byteswap(inplace=True) # Print the byteswapped array print(A) Output: [1.5 2.5 3.5][3.13984e-319 5.37543e-321 1.54939e-320]Example 2:byteswap() method raises error when the array contains string elements. Python3 import numpy as geek # a is an array of strings a = geek.array(["arka","soumen","simran"],dtype = np.int16) print(a.byteswap(True)) Output : ValueError Traceback (most recent call last) in () 1 import numpy as geek----> 2 a = geek.array(["arka","soumen","simran"],dtype = np.int16) 3 4 #a is an array of strings 5 ValueError: invalid literal for int() with base 10: 'arka' Comment More infoAdvertise with us Next Article NumPy ndarray.__irshift__() | Shift NumPy Array Elements to Right A ArkadipGhosh Follow Improve Article Tags : Python Python-numpy Python numpy-ndarray Practice Tags : python Similar Reads NumPy ndarray.__ilshift__() | Shift NumPy Array Elements to Left The ndarray.__ilshift__() method is an in-place left-shift operation. It shifts elements in the array to the left of the number of positions specified. Example Python3 import numpy as np gfg = np.array([1, 2, 3, 4, 5]) # applying ndarray.__ilshift__() method print(gfg.__ilshift__(2)) Output[ 4 8 12 1 min read Find length of one array element in bytes and total bytes consumed by the elements in Numpy In NumPy we can find the length of one array element in a byte with the help of itemsize . It will return the length of the array in integer. And in the numpy for calculating total bytes consumed by the elements with the help of nbytes. Syntax: array.itemsize  Return :It will return length(int) of 1 min read NumPy ndarray.__irshift__() | Shift NumPy Array Elements to Right The ndarray.__irshift__() method returns a new array where each element is right-shifted by the value that is passed as a parameter. Example Python3 import numpy as np gfg = np.array([1, 2, 3, 4, 5]) # applying ndarray.__irshift__() method print(gfg.__irshift__(2)) Output[0 0 0 1 1] SyntaxSyntax: nd 1 min read How to swap columns of a given NumPy array? Swapping columns of a NumPy array means exchanging the positions of two specified columns across all rows. For example, if you have a 3x3 array with values like [[0, 1, 2], [3, 4, 5], [6, 7, 9]] and you swap column 0 with column 2, the array becomes [[2, 1, 0], [5, 4, 3], [9, 7, 6]]. Letâs explore d 4 min read NumPy ndarray.T | Get View of Transposed Array The NumPy ndarray.T attribute finds the view of the transposed Array. It can transpose any array having a dimension greater than or equal to 2. It works similarly to the numpy.transpose() method but it is easy and concise to use. SyntaxSyntax: ndarray.T Returns Transpose of given arrayExamplesLet's 1 min read Python | Numpy ndarray.__array__() With the help of ndarray.__array__() method, we can create a new array as we want by giving a parameter as dtype and we can get a copy of an array that doesn't change the data element of original array if we change any element in the new one. Syntax : ndarray.__array__() Return : Returns either a ne 1 min read Like