![]() |
VOOZH | about |
numpy.random.shuffle() function randomly modify the order of elements in a sequence like a list or NumPy array in-place. This means the original array is changed and no new array is returned.
Example:
[5 2 4 1 3]
Explanation: The array is shuffled in place. Each run may produce a different permutation.
numpy.random.shuffle(arr)
Key Points:
Example 1: Shuffling a 2D array
[[1 2] [5 6] [3 4]]
Explanation: Only the rows are shuffled. The columns within each row remain unchanged.
Example 2: No return value
None
Explanation: shuffle() returns None because it modifies the array in place.
Example 3: Trying to shuffle immutable types
Output
TypeError: 'tuple' object does not support item assignment
Explanation: You can’t shuffle tuples because they are immutable. Convert it to a list or NumPy array first.