Generate random permutation of huge 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
Generating a random permutation of a huge list is straightforward only when the list comfortably fits in memory. Once the data becomes large, the real question is not just how to shuffle, but whether you truly need the entire permuted list materialized at once.
Use random.shuffle for In-Memory Lists
If the list already fits in memory and you can modify it in place, random.shuffle is the standard answer.
This uses an in-place Fisher-Yates style shuffle and runs in linear time. It is usually the right choice for an ordinary Python list.
When the List Is Huge
For very large datasets, two constraints appear:
- the list itself may already consume a lot of memory,
- creating another full copy just to permute it may be too expensive.
If the list exists in memory already, random.shuffle is still memory-efficient because it does not create a second list. But if your data is so large that it barely fits or does not fit at all, you need a different strategy.
Shuffle Indexes Instead of Heavy Objects
Sometimes the objects are large, but their indexes are small. In that case, shuffling indexes can be cheaper than moving the objects themselves around in multiple structures.
This is still an in-memory technique, but it can reduce overhead in pipelines where the original data is referenced elsewhere.
Use NumPy for Numeric Arrays
If your data is numeric and already lives in NumPy, numpy.random.permutation can be a good fit.
For array-heavy scientific workloads, NumPy is often faster and more compact than manipulating Python objects one by one.
If It Truly Does Not Fit, Change the Problem
A full random permutation of data that does not fit in memory is not a free operation. You may need to rethink the requirement:
- sample a subset instead of permuting everything,
- shuffle chunks and process chunk by chunk,
- use an external-memory algorithm backed by disk,
- store data in a system designed for distributed shuffle operations.
For example, if you only need a random subset rather than a full permutation, use random.sample:
That is a very different problem, but in practice it is often what people really need.
Beware of Converting Huge Iterables Too Early
One common mistake is to start with a generator or file stream, then convert it to a full list just to shuffle it. If the input is truly huge, that conversion may be the real bottleneck.
At that point, Python's shuffle API is not the issue. The data volume is.
Common Pitfalls
- Using
random.shuffleon data that does not fit in memory. - Creating an unnecessary second list when an in-place shuffle would work.
- Converting large generators or file streams into lists too early.
- Asking for a full permutation when a random sample would be enough.
- Forgetting that Python object-heavy lists consume much more memory than raw numeric arrays.
Summary
- Use
random.shufflefor large lists that already fit in memory and can be modified in place. - Shuffle indexes when moving the full objects directly is inconvenient.
- Use NumPy for large numeric arrays when your pipeline already depends on it.
- If the data truly does not fit, rethink the problem as sampling, chunking, or external-memory processing.
- For huge datasets, the bottleneck is usually memory model and data volume, not shuffle syntax.
Related reading
- Generate Unique ID from Alphanumeric String
- Generating a pseudorandom binary sequence where the same number does not occur more than twice in a row
- Generating a random DAG
- Generating a random, non-repeating sequence of all integers in .NET
- Generating a gaussian distribution with only positive numbers
- Generating all 5 card poker hands
- Generate UUID for Cassandra in Python
- Generating a PNG with matplotlib when DISPLAY is undefined

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.