Sorting list according to corresponding values from a parallel list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Sorting one list by the values in a second parallel list is a common Python task. The cleanest solution is usually to combine the related values temporarily, sort once, and then unpack the result if you still need separate lists.
What parallel lists mean
Parallel lists are separate lists where element i in one list belongs with element i in another. For example:
Here, names[1] belongs with scores[1], so sorting the names "by score" means keeping those pairings intact.
The most Pythonic approach: zip and sorted
A straightforward way is to zip the two lists into pairs, sort by the second element, and then extract what you need.
That prints:
If you want just the reordered names:
Sorting in descending order
Use reverse=True when higher values should come first.
This is common for leaderboards, ranking, and reporting.
Keeping the lists separate after sorting
If you truly need separate parallel lists afterward, unzip the sorted pairs.
That preserves the relationship while returning to the original two-list style.
Sorting indexes instead of data
Sometimes you do not want to rearrange the original lists directly. In that case, sort the indexes.
This is useful when several related arrays all need the same ordering.
A better long-term structure
If the data naturally belongs together, parallel lists may not be the best representation. A list of tuples or dictionaries is often easier to reason about.
That avoids repeatedly re-pairing separate lists and usually makes bugs less likely.
Small refinements that help in real code
If performance and readability matter, operator.itemgetter(1) can be a nice replacement for the lambda key when sorting pairs.
You can also add secondary sort rules when the parallel values tie. For example, sort by score first and then by name:
That gives deterministic ordering and is often useful in reports or leaderboards.
Common Pitfalls
The biggest mistake is sorting one list independently and forgetting to reorder the other list the same way. That destroys the relationship between the data.
Another issue is assuming the lists are aligned when they are not. If the lists have different lengths, zip silently stops at the shorter one, which may hide missing data.
It is also easy to keep parallel lists too long in a design where tuples, dataclasses, or dictionaries would be clearer.
Finally, remember that Python's sort is stable. If two keys are equal, their original order is preserved. That is often useful, but only if you know it is happening.
Summary
- Use
sorted(zip(list1, list2), key=...)to sort one list by values from another. - Unzip afterward if you still need separate parallel lists.
- Sort indexes when multiple arrays need the same ordering.
- Be careful not to break the relationship between corresponding elements.
- If the values always belong together, a list of tuples or records is often a better structure than parallel lists.

