Python
list-sorting
parallel-lists
data-structures
sorting-algorithms

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:

python
names = ["Alice", "Bob", "Charlie"]
scores = [67, 80, 55]

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.

python
1names = ["Alice", "Bob", "Charlie"]
2scores = [67, 80, 55]
3
4pairs = sorted(zip(names, scores), key=lambda item: item[1])
5print(pairs)

That prints:

python
[("Charlie", 55), ("Alice", 67), ("Bob", 80)]

If you want just the reordered names:

python
sorted_names = [name for name, score in pairs]
print(sorted_names)

Sorting in descending order

Use reverse=True when higher values should come first.

python
pairs = sorted(zip(names, scores), key=lambda item: item[1], reverse=True)
print(pairs)

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.

python
1pairs = sorted(zip(names, scores), key=lambda item: item[1])
2sorted_names, sorted_scores = map(list, zip(*pairs))
3
4print(sorted_names)
5print(sorted_scores)

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.

python
1names = ["Alice", "Bob", "Charlie"]
2scores = [67, 80, 55]
3
4order = sorted(range(len(scores)), key=scores.__getitem__)
5print(order)
6print([names[i] for i in order])

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.

python
1students = [
2    ("Alice", 67),
3    ("Bob", 80),
4    ("Charlie", 55),
5]
6
7students.sort(key=lambda item: item[1])
8print(students)

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.

python
1from operator import itemgetter
2
3pairs = sorted(zip(names, scores), key=itemgetter(1))
4print(pairs)

You can also add secondary sort rules when the parallel values tie. For example, sort by score first and then by name:

python
pairs = sorted(zip(names, scores), key=lambda item: (item[1], item[0]))
print(pairs)

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.

Course illustration
Course illustration

All Rights Reserved.