Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A list of pairs students = [("Asha", 72), ("Ravi", 85), ("Meera", 72), ("Kabir", 90)]. Sort it by mark from the highest, and for equal marks by name from A to Z. Write it with a lambda, and then with a def.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The key is a tuple: the mark with a minus sign (so that the biggest comes first), and the name.
With a lambda:
With a def:
Check. Kabir 90 comes first, then Ravi 85, then the two students with 72 in alphabetical order: Asha, Meera.
Answer: [('Kabir', 90), ('Ravi', 85), ('Asha', 72), ('Meera', 72)], with both versions.
Log in to join the discussion.
Loading…