What does sorted(['kiwi', 'fig', 'banana'], key=len) return?
-
A
['banana', 'kiwi', 'fig']
-
B
['fig', 'kiwi', 'banana']
Correct answer
-
C
['banana', 'fig', 'kiwi']
-
D
['fig', 'banana', 'kiwi']
Correct answer
B. ['fig', 'kiwi', 'banana']
18 views
Explanation
The key function sorts by string length: 3, 4, 6. Without key=len, sorting would be alphabetical, giving ['banana', 'fig', 'kiwi'].
Reference
Asked in: NTS, Python Developer Interviews
Doubts & discussion
Not sure why the answer is B? Ask here and get an explanation.
Ask a doubt about this MCQ