Shuffle two list at once with same order
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
When two lists contain aligned data, such as features and labels, they must be shuffled with the same permutation. Shuffling them independently destroys that alignment and silently corrupts the data, which makes this a correctness problem, not just a convenience issue.
The Wrong Approach
This is what not to do:
After this runs, there is no guarantee that each text still matches the correct label.
Zip, Shuffle, Unzip
For two lists, the simplest correct pattern is to zip them together, shuffle once, and unzip them afterward.
This works because each pair stays together during the shuffle.
Shared Index Permutation
If you have more than two aligned lists or arrays, generate one shuffled index order and apply it everywhere.
This scales better and makes it obvious that one permutation is being reused consistently.
In-Place Pair Shuffling
If you want to keep the paired structure, you can simply continue working with the zipped list after shuffling.
This is often the best option if the two-list representation is only temporary anyway.
Reproducibility Matters
In machine learning and testing, use a dedicated random generator with a seed rather than the process-wide global random state.
This makes the shuffle deterministic and easier to debug.
NumPy Arrays Need the Same Idea
If your data lives in NumPy arrays rather than Python lists, the principle is unchanged: create one permutation and apply it to every aligned array.
This is usually better than converting arrays to lists just to shuffle them.
Length Mismatch Should Fail Fast
Before shuffling, ensure the lists are actually aligned by length.
Validation matters because a length mismatch is already a data bug before shuffling begins.
Common Pitfalls
The biggest pitfall is calling random.shuffle() separately on each list. Even if both lists are shuffled "randomly," they are almost certainly no longer aligned.
Another issue is forgetting that zip() returns tuples, not lists. That is usually fine, but some code expects mutable lists after the shuffle.
Developers also forget to seed the random generator when reproducibility matters for tests or model training.
Finally, if the lists may be empty, be careful with zip(*pairs) because unzipping an empty list of pairs needs special handling.
Summary
- Keep aligned data under one shared permutation when shuffling.
- For two lists,
zip, shuffle, and unzip is the simplest solution. - For many aligned structures, shuffle indices once and reuse them everywhere.
- Use a seeded random generator when you need reproducible results.
- Validate list lengths before shuffling so data bugs fail early.
Related reading
- Shuffling a list of objects
- Shuffling a list of objects
- similarity between two vectors representing star graphs
- Simple Pull Message Queue
- shuffling two tensors in the same order
- Sibling package imports
- Simple way to find if two different lists contain exactly the same elements?
- Simple way to visualize a TensorFlow graph in Jupyter?

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.