Sort two lists together Python

You have first to zip your lists. The list that needs to be sorted in ascending order should be kept as the first parameter in the zip(). Use the sorted() function on the zipped element and then unzip it to get your lists as tuples.

If you want the list as output, you can convert the tuple to the list.

Here are examples to show how to sort multiple lists based on one list.

>>> a=[9,7,3,4,10,7,6,2,8]
>>> b=[29,17,3,14,110,47,86,12,18]
>>> c=[109,732,30,4,110,760,689,210,80]

>>> a,b,c=[list(v) for v in zip(*sorted(zip(a,b,c)))]
>>> a
[2, 3, 4, 6, 7, 7, 8, 9, 10]
>>> b
[12, 3, 14, 86, 17, 47, 18, 29, 110]
>>> c
[210, 30, 4, 689, 732, 760, 80, 109, 110]

>>> a,b,c=map(list, zip(*sorted(zip(a,b,c))))
>>> a
[2, 3, 4, 6, 7, 7, 8, 9, 10]
>>> b
[12, 3, 14, 86, 17, 47, 18, 29, 110]
>>> c
[210, 30, 4, 689, 732, 760, 80, 109, 110]
>>>

You can also use the argsort() function of the Numpy module to get the indices for sorting a list. You can then use those indices to sort all other lists.

Here is an example:

>>> import numpy as np
>>> a=[9,7,3,4,10,7,6,2,8]
>>> b=[29,17,3,14,110,47,86,12,18]
>>> c=[109,732,30,4,110,760,689,210,80]
>>> i=np.argsort(a)
>>> i
array([7, 2, 3, 6, 1, 5, 8, 0, 4])

>>> a,b,c=np.asarray(a)[i], np.asarray(b)[i], np.asarray(c)[i]
>>> a
array([ 2, 3, 4, 6, 7, 7, 8, 9, 10])
>>> b
array([ 12, 3, 14, 86, 17, 47, 18, 29, 110])
>>> c
array([210, 30, 4, 689, 732, 760, 80, 109, 110])
>>>