Last modified: Nov 10, 2026
Python Shuffle a List: Random Reordering Guide
When working with lists in Python, you may sometimes need to randomize the order of elements. This process is commonly referred to as shuffling a list. Whether you're creating a card game, implementing a random selection algorithm, or just need to distribute items randomly, Python provides several effective ways to shuffle a list.
Understanding List Shuffling
List shuffling is the process of rearranging the elements of a list in a random order. Each element has an equal probability of ending up in any position. This is different from sorting, which arranges elements in a specific order.
Python's built-in random module provides the most common way to shuffle a list. The main function for this purpose is random.shuffle().
Using random.shuffle() Method
The random.shuffle() function modifies the list in place, meaning it changes the original list rather than creating a new one.
import random
# Create a sample list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Original list:", numbers)
# Shuffle the list
random.shuffle(numbers)
print("Shuffled list:", numbers)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Shuffled list: [7, 2, 9, 1, 5, 3, 8, 4, 10, 6]
Notice that the shuffled list has a completely different order. Each time you run this code, you'll get a different result due to the random nature of shuffling.
Alternative Methods to Shuffle a List
While random.shuffle() is the most common method, there are other approaches you can use depending on your needs.
Using random.sample()
The random.sample() function returns a new list with elements in random order without modifying the original list.
import random
# Create a sample list
original_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']
# Create a shuffled copy
shuffled_list = random.sample(original_list, len(original_list))
print("Original list:", original_list)
print("Shuffled list:", shuffled_list)
Original list: ['apple', 'banana', 'cherry', 'date', 'elderberry']
Shuffled list: ['cherry', 'elderberry', 'apple', 'banana', 'date']
Using sorted() with random key
You can also shuffle a list by sorting it with a random key value.
import random
# Create a sample list
letters = ['a', 'b', 'c', 'd', 'e', 'f']
# Shuffle using sorted with random key
shuffled = sorted(letters, key=lambda x: random.random())
print("Original list:", letters)
print("Shuffled list:", shuffled)
Original list: ['a', 'b', 'c', 'd', 'e', 'f']
Shuffled list: ['d', 'a', 'f', 'b', 'e', 'c']
Shuffling a List of Lists
Often, you might need to shuffle a list that contains other lists. The same methods apply, but you need to be aware of how nested structures behave.
import random
# Create a list of lists
nested_list = [[1, 2], [3, 4], [5, 6], [7, 8]]
print("Original nested list:", nested_list)
# Shuffle the outer list
random.shuffle(nested_list)
print("Shuffled nested list:", nested_list)
Original nested list: [[1, 2], [3, 4], [5, 6], [7, 8]]
Shuffled nested list: [[5, 6], [1, 2], [7, 8], [3, 4]]
When you shuffle a list of lists, only the order of the outer lists changes, not the elements within each inner list.
Shuffling with Reproducible Results
Sometimes you may want to get the same shuffled result each time you run your code. You can achieve this by setting a seed value for the random number generator.
import random
# Set a seed for reproducibility
random.seed(42)
# Create a sample list
cards = ['Ace', 'King', 'Queen', 'Jack', '10']
# Shuffle the list
random.shuffle(cards)
print("Shuffled cards:", cards)
Shuffled cards: ['Queen', '10', 'Jack', 'Ace', 'King']
Now if you run this code again with the same seed, you'll get the same shuffled result. This is useful for testing and debugging.
Common Use Cases for Shuffling
Shuffling lists has many practical applications in programming:
- Card games: Shuffling a deck of cards before dealing
- Random sampling: Selecting items randomly from a larger set
- Data augmentation: Randomizing training data order in machine learning
- Testing: Randomizing test case order to ensure unbiased results
Understanding the Fisher-Yates Algorithm
The random.shuffle() function actually implements the Fisher-Yates algorithm, which is an efficient method for generating random permutations.
This algorithm works by iterating through the list from the last element to the first, and for each element, it swaps it with a randomly selected element from the portion of the list that hasn't been processed yet.
Shuffling Strings in a List
You can also shuffle lists containing strings. The process is identical to shuffling numerical lists.
import random
# Create a list of strings
names = ['Alice', 'Bob', 'Charlie', 'Diana', 'Edward']
print("Original names:", names)
# Shuffle the list
random.shuffle(names)
print("Shuffled names:", names)
Original names: ['Alice', 'Bob', 'Charlie', 'Diana', 'Edward']
Shuffled names: ['Charlie', 'Alice', 'Diana', 'Bob', 'Edward']
Handling Large Lists
When working with very large lists, consider the performance implications of different shuffling methods.
The random.shuffle() method is generally the most efficient for in-place shuffling. If you need a shuffled copy of a large list, random.sample() might be more memory-efficient depending on your use case.
Shuffling with NumPy
If you're working with numerical data, you might consider using NumPy's shuffle function, which is optimized for arrays.
import numpy as np
# Create a NumPy array
arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print("Original array:", arr)
# Shuffle the array
np.random.shuffle(arr)
print("Shuffled array:", arr)
Original array: [ 10 20 30 40 50 60 70 80 90 100]
Shuffled array: [ 50 60 30 10 70 40 90 20 100 80]
Conclusion
Shuffling a list in Python is a straightforward process with several available methods. The random.shuffle() function is the most commonly used approach for in-place shuffling.
Remember to consider whether you need to modify the original list or create a shuffled copy. For reproducible results, set a seed value using random.seed().
With these techniques, you can easily randomize the order of elements in any Python list, making your programs more dynamic and engaging.
If you need to work with chunks of shuffled data, check out our guide on Python Chunk a List into Batches: Easy Guide.
For manipulating nested structures after shuffling, consider reading about Python Flatten a Nested List: Easy Guide.
When you need to extract specific elements from a shuffled list, the Python Slice a List: Complete Beginner Guide will help you master list slicing techniques.