0

I have two lists in my program, for example:

[user1, user2, user3, user4, user5, user6, user7]

[32, 54, 2, 684, 23, 876, 12]

The first list is the username, and the second list is how many messages the user sent.

Is there a way to sort the second list in descending order whilst also rearranging the first list to correspond to the new order of the second list?

  • 2
    First of all, does this answer your question? [Sorting list based on values from another list?](https://stackoverflow.com/questions/6618515/sorting-list-based-on-values-from-another-list). Second, you might consider switching to a `dict` instead of 2 lists – Tomerikoo May 10 '20 at 18:31
  • `lst1, lst2 = zip(*sorted(zip(lst1, lst2), key=lambda k: -k[1]))` where `lst1` is your first list, `lst2` second – Andrej Kesely May 10 '20 at 18:33
  • `zip` function allows parallel iteration – Ahmed I. Elsayed May 10 '20 at 18:34

0 Answers0