Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Sort ["Meera", "asha", "Kabir", "Ravi"]. Why does asha come last, and how do you fix it?
key=str.lower sorts without caring about capital letters.
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, compare your working with the solution.
sorted(names) gives ['Kabir', 'Meera', 'Ravi', 'asha']. In the computer's order, every capital letter comes before every small letter, so asha is last.
Fix. Sort by the small-letter form of each name: sorted(names, key=str.lower) gives ['asha', 'Kabir', 'Meera', 'Ravi'].
Log in to join the discussion.
Loading…