In this tutorial, you’ll learn how to shuffle a list in Python using random.shuffle(), and how to shuffle strings, ranges, dictionaries, generators and NumPy arrays. Shuffling means putting the items in a random order, like shuffling a deck of cards.
| Function | Description |
|---|---|
random.shuffle(list1) | Shuffle a list in place (preferred way) |
random.sample(seq, len(seq)) | Return a new shuffled list and leave the original unchanged; also works with immutable sequences such as strings and ranges |
rng.shuffle(array) | Shuffle a NumPy array in place (along the first axis) |
rng.permutation(array) | Return a shuffled copy of a NumPy array |
Table of contents
- The random.shuffle() function
- Shuffle a list
- Shuffle a list without changing the original
- Shuffle two lists in the same order
- Shuffle a NumPy multidimensional array
- Get the same shuffle result every time
- Shuffle a string
- Shuffle a range of integers
- Shuffle a dictionary
- Shuffle a generator
- Limitations of random.shuffle()
- Summary
The random.shuffle() function
Syntax:
random.shuffle(x)Code language: Python (python)
x: a mutable sequence, usually a list.- It shuffles
xin place and returnsNone.
Note: Older Python versions accepted a second argument, random, a custom function returning floats. It was deprecated in Python 3.9 and removed in 3.11. Passing it now raises a TypeError.
Shuffle a list
Use the below steps to shuffle a list in Python
- Create a list
Create the list you want to shuffle. For example,
list1 = [10, 20, 'a', 'b'] - Import random module
Import the random module using
import random. - Use the shuffle() function of a random module
Call
random.shuffle(list1)to shufflelist1in place. It changes the order of items in the original list and doesn’t return a new list. - Display the shuffled list
shuffle()returnsNone, so print the list itself:print(list1). - Get a shuffled list instead of modifying the original list
Shuffle a copy of the list, or use random.sample() to get a new shuffled list and keep the original unchanged.
Example:
Output (your order will differ):
[42, 70, 28, 35, 49, 56, 21, 63, 14, 7] None [14, 70, 49, 63, 21, 56, 35, 28, 42, 7]
Common mistake: new_list = random.shuffle(old_list) sets new_list to None. Shuffle first, then use the list.
Shuffle a list without changing the original
Because shuffle() changes the original list, use one of these options when you need to keep it.
Option 1: Make a copy of the original list
Output:
Original list: [33, 66, 99, 188, 256, 412, 748] Shuffled list: [188, 412, 66, 748, 33, 99, 256]
Option 2: Use random.sample()
random.sample() returns a new list of k random items. When k equals the length of the list, the result is a shuffled copy.
Output:
[99, 188, 412, 66, 748, 33, 256]
Shuffle two lists in the same order
Suppose one list holds employee names and another holds their salaries. To shuffle both while keeping each name next to its salary, zip() them into pairs, shuffle the pairs, then unzip them.
Output:
['Jason', 'Kelly', 'Jon', 'Emma'] [10000, 9000, 7000, 6500]
Shuffle a NumPy multidimensional array
Create a NumPy Generator with default_rng(). Its shuffle() method shuffles an array in place along the first axis, so the rows of a 2D array change order but each row stays intact. Use permutation() to get a shuffled copy instead.
Output:
Before shuffling: [[100 110] [120 130] [140 150]] After shuffling: [[140 150] [120 130] [100 110]] [4 1 3 2 5]
Get the same shuffle result every time
Python’s random module is a pseudo-random number generator (PRNG): its output is fully determined by a starting value called the seed. By default, the seed comes from the operating system. If you set the seed yourself, the same list always shuffles into the same order.
Output:
[40, 60, 50, 10, 30, 20] [40, 60, 50, 10, 30, 20] [40, 60, 50, 10, 30, 20]
Note: Shuffle a fresh copy each time. If you re-shuffle the already shuffled list, the same seed produces a different final order because the starting order is different.
Shuffle a string
Strings are immutable, so random.shuffle() can’t change them and raises TypeError: 'str' object does not support item assignment. Convert the string to a list of characters, shuffle it, and join it back. Or use sample() to do it in one line.
Output:
tYeaivPn PYtinvea
Shuffle a range of integers
A range is immutable too, so shuffle(range(10)) raises a TypeError. Convert it to a list first.
Output:
[8, 5, 9, 7, 1, 2, 6, 4, 0, 3]
Shuffle a dictionary
You can’t shuffle a dictionary directly, but since Python 3.7 dictionaries keep insertion order. So shuffle the keys and build a new dictionary in that order.
Output:
{'Mike': 66, 'Scott': 75, 'Eric': 80, 'Jessa': 95}
Shuffle a generator
shuffle() needs to know the length of the sequence, so a generator raises TypeError: object of type 'generator' has no len(). Convert the generator to a list first.
Output:
[50, 25, 75, 100]
Limitations of random.shuffle()
- Long sequences: The number of possible orders grows very fast. For lists longer than about 2,080 items, there are more possible orders than the Mersenne Twister generator can produce, so some orders can never appear. This rarely matters in practice.
- Security: The random module is predictable. To shuffle for anything security-sensitive, use
random.SystemRandom().shuffle(x). See cryptographically secure random data in Python.
Practice Problem:
Create a deck of 52 cards (ranks 2–10, J, Q, K, A in four suits), shuffle it, and deal 5 cards.
Show Solution
Summary
random.shuffle(x)shuffles a list in place and returnsNone.- To keep the original, shuffle a copy or use
random.sample(x, k=len(x)). - Convert strings, ranges, dictionary keys and generators to a list before shuffling.
- Use
random.seed()for repeatable shuffles and NumPy’srng.shuffle()orrng.permutation()for arrays.
Now test what you’ve learned with the Python random data generation exercise and quiz.
