1

So, I am trying to sort 2 related lists. One contains Candidate Names, and one contains the Number of Votes the candidates have (candidate in candidate[0] has votes stored in votes[0]).

I found a way to sort votes[] in descending order, whilst keeping the indexes matched. For example if vote[0] becomes vote[3], candidate[0] will also become candidate[3]. I did this using the built-in zip function, but the example I copied from sorted the lists in ascending order, whereas I require them to be sorted in descending order. Here are my lists:

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]

And to sort the lists I used:

votes, candidates = (list(t) for t in zip(*sorted(zip(votes, candidates))))

This did exactly what I wanted, except in ascending order, not descending. How do I edit this to sort the lists into Descending order?

2
  • Those aren't nested lists, in fact aren't even lists Commented Feb 12, 2019 at 13:38
  • 2
    sorted(zip(votes, candidates), reverse=True) Commented Feb 12, 2019 at 13:40

3 Answers 3

2

You could just sort the zip of both the list and reverse it

>>> votes = [9, 7, 1, 3]
>>> candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
>>> 
>>> sorted(zip(votes, candidates), key=lambda x: x[0]) # in ascending order
[(1, 'Hillary'), (3, 'Mitt'), (7, 'Barack'), (9, 'Donald')]
>>>
>>> sorted(zip(votes, candidates), key=lambda x: x[0], reverse=True) # in descending order
[(9, 'Donald'), (7, 'Barack'), (3, 'Mitt'), (1, 'Hillary')]

>>> # and if you want it back in order again;
>>> votes, names = zip(*sorted(zip(votes, candidates), key=lambda x: x[0], reverse=True))
>>> votes
(9, 7, 3, 1)
>>> names
('Donald', 'Barack', 'Mitt', 'Hillary')
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to get it to output two lists, like unzipping it?
Sure. But why ? The dataset seems to be closely related ?
Anyway, here you go, votes, names = zip(*sorted(zip(votes, candidates), key=lambda x: x[0], reverse=True))
0

using reverse=True:

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]

votes, candidates = (list(t) for t in zip(*sorted(zip(votes, candidates))))


print(sorted(votes, reverse=True))
print(sorted(candidates, reverse=True))

OUTPUT:

[9, 7, 3, 1]
['Mitt', 'Hillary', 'Donald', 'Barack']

OR

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]

print(sorted(zip(candidates, votes), reverse=True))

OUTPUT:

[('Mitt', 3), ('Hillary', 1), ('Donald', 9), ('Barack', 7)]

EDIT 3:

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]

votes, candidates = (list(t) for t in zip(*sorted(zip(votes, candidates), reverse=True)))

print(votes)
print(candidates)

OUTPUT:

[9, 7, 3, 1]
['Donald', 'Barack', 'Mitt', 'Hillary']

4 Comments

This outputs 2 lists, which is perfect. BUT, the lists are not in sync. Donald should be first, then Barack etc
@DaneBaird why should Donald be first if it is in descending order?
@DaneBaird check Edit 3.
Sorry about that, I realise I didnt specify which list I wanted to sort first! EDIT 3 is perfect
0

Or, slicing for reversing:

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]
votes, candidates = (list(t) for t in zip(*sorted(zip(votes, candidates))[::-1]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.