Sort two lists one string other float

I have two lists

one = ['allen', 'steve', 'george', 'max']
two = [88.63, 33.99, 72.66, 92.11]

I would like two lists sorted as below.

one = ['max', 'allen', 'george', 'steve']
two = [92.11, 88.63, 72.66 33.99]

What I have tried

one, two = sorted(zip(one, two))

2 answers

  • answered 2021-10-12 15:59 Dobos Imre

    One way of doing that:

    one, two = zip(*sorted(zip(one, two), key=lambda tup: tup[1], reverse=True))

  • answered 2021-10-12 16:00 nicholas

    You could combine the lists into a matrix and then sort the matrix using the numbers. The matrix is a 2D list that would group each number with a name. You can make this matrix using a list comprehension

    combinedList = [[one[x], two[x]] for x in range(len(two))]


    [['max', 92.11], ['allen', 88.63], ['george', 72.66], ['steve', 33.99]]

    Then, sort the list using the 2nd item in each entry (index 1)

    outputList = sorted(combinedList, key=lambda x: x[1])


    [['steve', 33.99], ['george', 72.66], ['allen', 88.63], ['max', 92.11]]

    For further processing, you could once again split this list back into list one and two, but sorted. Again, using list comprehension

    one = [x[0] for x in outputList]
    two = [x[1] for x in outputList]


    ['steve', 'george', 'allen', 'max']
    [33.99, 72.66, 88.63, 92.11]

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum