Every now and then, I need to sort a pair of Python language parallel arrays. When I google for an example, I always see Internet results that use wonky syntax like:
import operator result = [k for k, v in sorted(zip(list_a, list_b), \ key=operator.itemgetter(1))]
When I need to sort parallel arrays, I use the np.argsort() function, which is much cleaner in my opinion.
Suppose you have an array of arrays where each array is a potential solution to some problem. For example, one potential solution might be [4, 3, 0, 3]. So suppose you have five such potential solutions:
import numpy as np
solns = np.array([[4,3,0,3],
[1,3,1,2],
[0,1,0,3],
[4,3,2,1],
[2,2,2,0]], dtype=np.int64)
And you have a parallel array that holds the associated errors for each potential solution:
errors = np.array([10.0, 7.0, 4.0, 10.0, 6.0], dtype=np.float32)
For simplicity, I made the error for each potential solution the sum of the values in the solution, but in a non-demo scenario the errors would likely be computed using some other function.
To sort the solutions and errors arrays, you can call np.argsort() on the errors array. This will give you the indexes if the errors array was sorted, but won’t actually sort the array. Then you can apply the indexes to the solutions and errors arrays:
idxs = np.argsort(errors) # [2, 4, 1, 0, 3] solns = solns[idxs] # now sorted by error errors = errors[idxs] # sorted by error
and viola! Both arrays are sorted in parallel.
One scenario where parallel arrays can be used is in Evolutionary Algorithms. One parallel array holds possible solution information (typically a vector) to some problem, and the other parallel array holds the error value associated with each possible solution. There are several other designs, including 1.) using a Python class with solution and error fields, 2.) a Python List or NumPy Array where each item is a list or array with a possible solution and the associated error, 3.) a Python Tuple where a possible solution is the first element and the associated error is the second element. Each design has pros and cons. There’s a trade-off between design storage simplicity and the sorting (by error) simplicity.

Jumping over track and field hurdles is prone to error. Here are three examples: high error, low error, puppy error.
Demo program code:
# parallel_sort_demo.py
import numpy as np
solns = np.array([[4,3,0,3],
[1,3,1,2],
[0,1,0,3],
[4,3,2,1],
[2,2,2,0]], dtype=np.int64)
errors = np.array([10.0, 7.0, 4.0, 10.0, 6.0],
dtype=np.float32)
print("\nsolns: ")
print(solns)
print("errors: ")
print(errors)
idxs = np.argsort(errors) # [2, 4, 1, 0, 3]
solns = solns[idxs]
errors = errors[idxs]
print("\nsorted solns: ")
print(solns)
print("sorted errors: ")
print(errors)

.NET Test Automation Recipes
Software Testing
SciPy Programming Succinctly
Keras Succinctly
R Programming
2026 Visual Studio Live
2025 Summer MLADS Conference
2026 DevIntersection Conference
2025 Machine Learning Week
2025 Ai4 Conference
2026 G2E Conference
2026 iSC West Conference
You must be logged in to post a comment.