Skip to content

How to Randomly Shuffle Elements in Python Lists

How to shuffle Python lists using random.shuffle() for in-place changes, random.sample() for creating shuffled copies, and NumPy for efficiently handling large datasets.

Shuffling a list is a fundamental operation in game development, data science, and simulation tasks. Whether you need to randomize a deck of cards, split a dataset for machine learning, or simply randomize quiz questions, Python provides efficient built-in tools to handle this.

This guide explores the standard random.shuffle() method, techniques to create a new shuffled list without modifying the original, and high-performance options using NumPy.

Method 1: In-Place Shuffling (random.shuffle)

The most common way to shuffle a list is using the random.shuffle() function. This method modifies the original list directly and returns None.

import random

numbers = [1, 2, 3, 4, 5]

random.shuffle(numbers)

print(f"Shuffled list: {numbers}")

Output:

Shuffled list: [3, 1, 5, 2, 4]

Method 2: Creating a New Shuffled List (random.sample)

If you need to preserve the original order of your list while creating a randomized version, use random.sample(). By sampling len(list) elements, you effectively create a full, shuffled copy.

import random

original = ['apple', 'banana', 'cherry', 'date']

shuffled_copy = random.sample(original, k=len(original))

print(f"Original: {original}")
print(f"Shuffled: {shuffled_copy}")

Output:

Original: ['apple', 'banana', 'cherry', 'date']
Shuffled: ['apple', 'cherry', 'date', 'banana']

Method 3: High-Performance Shuffling (NumPy)

For Data Science and Machine Learning tasks involving large datasets, the numpy library is significantly faster than standard Python lists.

import numpy as np

data = np.array([10, 20, 30, 40, 50])

np.random.shuffle(data)

print(f"NumPy Shuffle: {data}")

Output:

NumPy Shuffle: [30 50 10 40 20]

Reproducible Shuffling (Seeding)

In debugging or scientific experiments, you often need the "random" order to be the same every time you run the script. This is achieved by setting a seed.

import random

data = [1, 2, 3, 4, 5]

random.seed(42)
random.shuffle(data)
print(f"Run 1: {data}")

data = [1, 2, 3, 4, 5]
random.seed(42)
random.shuffle(data)
print(f"Run 2: {data}")

Output:

Run 1: [4, 2, 3, 5, 1]
Run 2: [4, 2, 3, 5, 1]

Common Pitfalls

Assigning the Result of random.shuffle

A very common mistake is assigning the result of shuffle() to a variable. Since shuffle() modifies the list in-place, it returns None.

import random

cards = ['J', 'Q', 'K', 'A']

deck = random.shuffle(cards)
print(f"Deck variable: {deck}")

random.shuffle(cards)
print(f"Cards variable: {cards}")

Output:

Deck variable: None
Cards variable: ['J', 'Q', 'K', 'A']

Attempting to Shuffle Immutable Sequences

You cannot shuffle tuples or strings because they are immutable (cannot be changed).

import random

my_tuple = (1, 2, 3)

try:
    random.shuffle(my_tuple)
except TypeError as e:
    print(f"Error: {e}")

temp_list = list(my_tuple)
random.shuffle(temp_list)
new_tuple = tuple(temp_list)
print(f"Shuffled Tuple: {new_tuple}")

Output:

Error: 'tuple' object does not support item assignment
Shuffled Tuple: (3, 2, 1)

Conclusion

To randomly shuffle lists in Python:

  1. Use random.shuffle(lst) if you want to modify the list directly and save memory.
  2. Use random.sample(lst, len(lst)) if you need to keep the original list intact.
  3. Use random.seed() if you need the random order to be reproducible for testing.
  4. Avoid assigning the result of random.shuffle() to a variable, as it will be None.
Navigation