← Quiz: Counting Sort and Custom Sorting: all 13 questions
Quiz: Counting Sort and Custom Sorting · Question 11 of 13
What does this print? (code)
What does this print?
words = ["kiwi", "fig", "banana", "apple"]
print(sorted(words, key=lambda w: (len(w), w)))
- A.['apple', 'banana', 'fig', 'kiwi']
- B.['fig', 'kiwi', 'banana', 'apple']
- C.['banana', 'apple', 'kiwi', 'fig']
- D.['fig', 'kiwi', 'apple', 'banana']
Answer & explanation
Correct answer: ['fig', 'kiwi', 'apple', 'banana']
The tuple key sorts by length first (3, 4, 5, 6) and uses the word itself only to break ties.
Can you get the rest right without peeking?
Take the timed Quiz: Counting Sort and Custom Sorting quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →