random.shuffle() Function Explained
random.shuffle() puts the items in a list into a random order.
It is important to know two things about this function:
- It changes the original list
- It returns
None
This is a common source of beginner mistakes. If you expect a new list to be returned, your code will not work the way you expect.
Quick example
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)
Example output:
[3, 1, 5, 2, 4]
random.shuffle() changes the original list and returns None.
What random.shuffle() does
random.shuffle() is used when you want to reorder all items in a list randomly.
Key points:
- It shuffles the items in a list into a random order
- It works in place, which means it changes the original list
- It does not create a new shuffled list
- It returns
None
If you are new to the random module, see the random module overview.
Basic syntax
Use this form:
import random
random.shuffle(my_list)
Things to remember:
- You must import the
randommodule first - You should pass a mutable sequence such as a list
- The list is changed directly
What gets changed
When you call random.shuffle(my_list), the original list is modified.
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)
Example output:
[4, 1, 5, 3, 2]
If another variable points to the same list, it will also see the changed order.
import random
numbers = [1, 2, 3, 4, 5]
other_name = numbers
random.shuffle(numbers)
print(numbers)
print(other_name)
Possible output:
[2, 5, 1, 4, 3]
[2, 5, 1, 4, 3]
This happens because both variables refer to the same list object.
If this idea is unfamiliar, it helps to understand mutability in Python.
Simple example
Here is a basic example with a short list:
import random
letters = ["a", "b", "c", "d"]
random.shuffle(letters)
print(letters)
Possible output:
['c', 'a', 'd', 'b']
If you run the program again, the order will usually be different.
That is normal. The purpose of shuffle() is to produce a random ordering.
How to keep the original list unchanged
If you need to keep the original order, make a copy first and shuffle the copy.
You can copy a list with .copy():
import random
original = [1, 2, 3, 4, 5]
shuffled = original.copy()
random.shuffle(shuffled)
print("Original:", original)
print("Shuffled:", shuffled)
Possible output:
Original: [1, 2, 3, 4, 5]
Shuffled: [4, 1, 5, 2, 3]
You can also copy with slicing:
import random
original = [1, 2, 3, 4, 5]
shuffled = original[:]
random.shuffle(shuffled)
print("Original:", original)
print("Shuffled:", shuffled)
This is useful when you need both:
- the original list
- a random version of the same items
If you want to learn more about copying lists, see list.copy().
Common beginner confusion
random.shuffle(my_list) does not return the shuffled list
This is one of the most common mistakes:
import random
numbers = [1, 2, 3, 4, 5]
new_list = random.shuffle(numbers)
print(new_list)
print(numbers)
Output:
None
[2, 4, 1, 5, 3]
Why this happens:
shuffle()changesnumbersdirectly- the return value is
None - so
new_listbecomesNone
The correct way is:
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)
You cannot shuffle a tuple or string in place
random.shuffle() is meant for mutable data, usually lists.
This will not work as expected with a tuple:
import random
values = (1, 2, 3, 4)
random.shuffle(values)
A tuple cannot be changed in place.
A string also cannot be shuffled in place because strings are immutable.
If you need to shuffle a tuple or string:
- Convert it to a list
- Shuffle the list
- Convert it back if needed
Example with a tuple:
import random
values = (1, 2, 3, 4)
temp_list = list(values)
random.shuffle(temp_list)
print(temp_list)
Example with a string:
import random
text = "hello"
chars = list(text)
random.shuffle(chars)
shuffled_text = "".join(chars)
print(shuffled_text)
When to use random.shuffle() vs random.choice()
These two functions solve different problems.
Use random.shuffle() when you want to:
- reorder all items in a list
- get a random arrangement of the whole list
Use random.choice() when you want to:
- pick one random item
- leave the original list order unchanged
Example:
import random
numbers = [10, 20, 30, 40]
item = random.choice(numbers)
print("Random item:", item)
random.shuffle(numbers)
print("Shuffled list:", numbers)
Possible output:
Random item: 30
Shuffled list: [20, 40, 10, 30]
If you want to learn that function next, see random.choice() explained.
Common mistakes
Here are the most common problems beginners run into with random.shuffle():
- Assigning the result of
random.shuffle()to a variable and gettingNone - Trying to shuffle a tuple or string
- Forgetting to
import random - Expecting the same shuffled order every time without setting a seed
These quick checks can help when debugging:
import random
print(my_list)
result = random.shuffle(my_list)
print(result)
print(type(my_list))
print(len(my_list))
What these lines help you check:
- whether your list contains the values you expect
- whether
shuffle()returnedNone - whether your variable is actually a list
- whether the list has items in it
If you want a task-focused example, see how to shuffle a list in Python.
FAQ
Does random.shuffle() return a new list?
No. It changes the original list and returns None.
Can I use random.shuffle() on a tuple?
No. Tuples cannot be changed in place. Convert the tuple to a list first.
Why is my variable None after using random.shuffle()?
Because shuffle() returns None. It modifies the list directly.
How do I shuffle without changing the original list?
Copy the list first, then shuffle the copy.