Question 2
Back to Exercise
What is the output of the following Python program?
words = ["kiwi", "fig", "banana", "apple", "date"]
print(sorted(words, key=lambda w: (len(w), w)))
- A ['fig', 'kiwi', 'date', 'apple', 'banana']
- B ['apple', 'banana', 'date', 'fig', 'kiwi']
- C ['banana', 'apple', 'kiwi', 'date', 'fig']
- D ['fig', 'date', 'kiwi', 'apple', 'banana'] Correct
Correct Answer: Option D
The tuple key sorts by length first; "date" and "kiwi" both have 4 letters and are then ordered alphabetically.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!