1

I have a list of all countries in the world with its population e.g.

Countries = [("argentina", 124854), ("brazil",568854)]

The first value is a string=country the second is integer = population

The task is sort the list by it second value by using reverse true when sorted, I have managed to do it by the string first value country in analphabetic order ASC and DESC but I don't know how to write the code to sort by population in from the largest to smallest population?

pop lambda country:country[1]
countries.sort(key=pop, reverse=True)
Countries

This code doesn't work as expected.

1
  • "This code doesn't work as expected." What happens instead? Commented Jun 15, 2020 at 0:56

1 Answer 1

1
Countries = [("argentina", 124854), ("brazil",568854)]

print(sorted(Countries, key=lambda k: k[1], reverse=True))

Prints:

[('brazil', 568854), ('argentina', 124854)]

Or:

print(sorted(Countries, key=lambda k: -k[1]))
Sign up to request clarification or add additional context in comments.

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.