🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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)))
  1. A.['apple', 'banana', 'fig', 'kiwi']
  2. B.['fig', 'kiwi', 'banana', 'apple']
  3. C.['banana', 'apple', 'kiwi', 'fig']
  4. 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 →