Python:NumPy byteswap()
The byteswap() method reverses the byte order of every element in a NumPy array. This is used when converting data between systems with different endianness (byte-ordering conventions). The operation either returns a new array or modifies the original one in place when inplace=True.
Syntax
ndarray.byteswap(inplace=False)
Parameters:
inplace(optional): When set toTrue, the byte order of the existing array is swapped in place. WhenFalse, a new array with swapped bytes is returned.
Return value:
Returns a new ndarray with swapped byte order, unless inplace=True, in which case the original array is modified and returned.
Example 1
In this example, the array’s byte order is swapped to convert the data into the opposite endianness:
import numpy as nparr = np.array([1, 256, 1024], dtype=np.int32)print("Original array:")print(arr)print("Original dtype:", arr.dtype)swapped = arr.byteswap()print("\nAfter byteswap():")print(swapped)print("Swapped dtype:", swapped.dtype)
The output of this code is:
Original array:[ 1 256 1024]Original dtype: int32After byteswap():[16777216 65536 262144]Swapped dtype: int32
Example 2
This example demonstrates byteswap(inplace=True) and shows how the original data is altered directly:
import numpy as nparr = np.array([100, 200, 300], dtype=np.int32)print("Before inplace byteswap:", arr)arr.byteswap(inplace=True)print("After inplace byteswap:", arr)
The output of this code is:
Before inplace byteswap: [100 200 300]After inplace byteswap: [1677721600 -939524096 738263040]
Codebyte Example
Use the codebyte below to inspect how byteswap() affects a 2-D array and observe the internal memory representation change:
Frequently Asked Questions
1. What is the function of byteswap() in Python?
The byteswap() method reverses the byte order of every element in a NumPy array. It is commonly used when preparing data for systems with different endianness or when interpreting binary data from external sources.
2. What are bytes and bytearrays in Python?
A bytes object in Python is an immutable sequence of byte values, while a bytearray is a mutable version of the same concept. Both store raw binary data and are often used for file handling, networking, and low-level memory operations.
3. How to shuffle a NumPy ndarray?
A NumPy array can be shuffled using np.random.shuffle() for in-place row-wise shuffling or np.random.permutation() to return a shuffled copy. These functions randomize the order of elements while preserving the array’s structure.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours