Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Sort ["banana", "fig", "cherry", "kiwi"] by length, shortest first and then longest first.
key=len sorts by the length of each item.
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(words, key=len) gives ['fig', 'kiwi', 'banana', 'cherry'].reverse=True: ['banana', 'cherry', 'kiwi', 'fig'].banana and cherry have the same length, so they stay in their old order.
Log in to join the discussion.
Loading…