🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.random.shuffle()

AI & DATA SCIENCE // np-random-shuffle

np.random.shuffle() randomly reorders the elements of an array in place, modifying the original array directly rather than returning a new one.

Syntax

np.random.shuffle(arr)

Deep Dive Course

shuffle() mutates its argument directly and returns None — a common mistake is writing an assignment like `arr = np.random.shuffle(arr)`, which sets arr to None instead of the shuffled array, since shuffle() doesn't return anything to reassign. For a multi-dimensional array, shuffle() only reorders along the first axis, shuffling entire rows relative to each other rather than shuffling every individual element throughout the array.

1Understanding np.random.shuffle()

shuffle() mutates its argument directly and returns None — a common mistake is writing an assignment like arr = np.random.shuffle(arr), which sets arr to None instead of the shuffled array, since shuffle() doesn't return anything to reassign. For a multi-dimensional array, shuffle() only reorders along the first axis, shuffling entire rows relative to each other rather than shuffling every individual element throughout the array.

💡

Never write `arr = np.random.shuffle(arr)` — shuffle() modifies the array in place and returns None, so that assignment overwrites arr with None; just call np.random.shuffle(arr) on its own line.

editor.html
import numpy as np

np.random.seed(0)
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)
localhost:3000

2Practical Example

Here is a real-world application of np.random.shuffle() showing how it is used in production NumPy code.

editor.html
import numpy as np

np.random.seed(0)
matrix = np.array([[1, 2], [3, 4], [5, 6]])
np.random.shuffle(matrix)
print(matrix)
localhost:3000

3Best Practices

Follow these guidelines when working with np.random.shuffle():

1. Call shuffle() as a standalone statement, not as the right-hand side of an assignment, since it returns None

2. Use np.random.permutation() instead of shuffle() when you need a shuffled copy and want to leave the original array untouched

3. Remember shuffle() on a multi-dimensional array only reorders along the first axis, whole rows, not every individual element

⚠️

Tip: Never write `arr = np.random.shuffle(arr)` — shuffle() modifies the array in place and returns None, so that assignment overwrites arr with None; just call np.random.shuffle(arr) on its own line.

editor.html
import numpy as np

np.random.seed(0)
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

np.random.seed(0)
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)
Example 02Advanced Example
import numpy as np

np.random.seed(0)
matrix = np.array([[1, 2], [3, 4], [5, 6]])
np.random.shuffle(matrix)
print(matrix)

Best Practices

  • Call shuffle() as a standalone statement, not as the right-hand side of an assignment, since it returns None
  • Use np.random.permutation() instead of shuffle() when you need a shuffled copy and want to leave the original array untouched
  • Remember shuffle() on a multi-dimensional array only reorders along the first axis, whole rows, not every individual element

Interview Question

Why does writing `arr = np.random.shuffle(arr)` end up setting arr to None?

Hint: Think about what shuffle() actually returns.

shuffle() reorders the array's elements in place, directly mutating the original array object, and its return value is always None, matching the convention many in-place mutating methods follow, similar to a Python list's own sort() method. Assigning shuffle()'s return value back to arr overwrites the variable with None, discarding the reference to the already-shuffled array, which is why shuffle() should simply be called on its own line without capturing or reassigning its result.

Exercises

MediumPractice using np.random.shuffle() in a real scenario.
View Solution
import numpy as np

np.random.seed(0)
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)

Frequently Asked Questions

Why does writing `arr = np.random.shuffle(arr)` end up setting arr to None?

shuffle() reorders the array's elements in place, directly mutating the original array object, and its return value is always None, matching the convention many in-place mutating methods follow, similar to a Python list's own sort() method. Assigning shuffle()'s return value back to arr overwrites the variable with None, discarding the reference to the already-shuffled array, which is why shuffle() should simply be called on its own line without capturing or reassigning its result.

Related Functions

np-random-permutationnp-random-choicelists