Specific shuffling list in Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Python's random.shuffle() randomizes a list in place, but many real-world scenarios require specific shuffling patterns — reproducible shuffles with a seed, shuffling without modifying the original, shuffling groups of elements together, or interleaving lists in a specific pattern. This article covers standard shuffling, seeded shuffles, partial shuffles, group-preserving shuffles, and custom shuffle algorithms.
Basic Shuffle (In-Place)
random.shuffle() modifies the list in place and returns None. It uses the Fisher-Yates algorithm, which produces a uniform distribution of all permutations.
Shuffle Without Modifying the Original
Use random.sample() to get a shuffled copy:
Or copy first, then shuffle:
Reproducible Shuffle (Seeded)
Set a seed so the shuffle produces the same result every time:
Useful for testing, reproducible experiments, and debugging.
Shuffle with Custom Random Generator
Use an independent Random instance to avoid affecting global state:
This is important in multithreaded code or when you need multiple independent random streams.
Partial Shuffle (Shuffle First N Elements Only)
Shuffle only a portion of the list:
Group-Preserving Shuffle
Shuffle groups of elements together (e.g., keep pairs intact):
Constrained Shuffle (No Element in Original Position)
A derangement ensures no element remains in its original position:
This brute-force approach is efficient for small lists. For large lists, use the Sattolo algorithm.
Weighted Shuffle
Shuffle with bias — elements with higher weights appear earlier:
Interleave Two Lists
Merge two lists in alternating order (a specific "shuffle" pattern):
Shuffle a Dictionary's Values
Shuffle values while keeping keys intact:
Common Pitfalls
- Assigning the return value of
shuffle():random.shuffle()returnsNoneand modifies the list in place.result = random.shuffle(items)setsresulttoNone. Userandom.sample(items, len(items))for a returned shuffled copy. - Shuffling immutable sequences:
random.shuffle()requires a mutable sequence. Passing a tuple or string raisesTypeError. Convert to a list first:list("hello"), then shuffle. - Global seed affecting other random calls:
random.seed(42)affects all subsequent calls to therandommodule. Userandom.Random(42)for an independent generator that does not interfere with other random operations. - Shuffling a generator:
random.shuffle()needs indexed access (__getitem__). Generators and iterators do not support this. Convert to a list first:items = list(generator). - Non-uniform shuffle from a bad algorithm: Implementing shuffle manually (e.g., swapping each element with a random position from the entire array) produces a biased distribution. Always use Fisher-Yates (which
random.shuffleimplements) orrandom.sample().
Summary
random.shuffle(lst)shuffles in place (returnsNone) using the Fisher-Yates algorithmrandom.sample(lst, len(lst))returns a new shuffled list without modifying the original- Use
random.seed(n)orrandom.Random(n)for reproducible shuffles - Chunk the list for group-preserving shuffles, or loop until a derangement for constrained shuffles
- Use weighted random keys for priority-biased shuffling
- Always use
random.shuffle()orrandom.sample()instead of custom swap algorithms to ensure uniform distribution
Related reading
- Split / Explode a column of dictionaries into separate columns with pandas
- Split a binary search Tree
- Split a List into smaller lists of N size
- Split a list of numbers into n chunks such that the chunks have close to equal sums and keep the original order
- Specify extras_require with pip install -e
- Specify list of possible values for Pandas get_dummies
- Split a python list into other sublists i.e smaller lists
- Split a String into an array in Swift?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.