ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
print(sorted(['b10', 'b9', 'a10'], key=lambda s: (s[0], int(s[1:]))))
  1. A. ['a10', 'b10', 'b9']
  2. B. ['a10', 'b9', 'b11']
  3. C. ['a10', 'b9', 'b10']
  4. D. ['b9', 'b10', 'a10']

Correct answer: C. ['a10', 'b9', 'b10']

The key takes the letter first and then the number as an int, so b9 comes before b10 even though '9' is greater than '1' as text.

More Python Programming questions

Exams that ask this topic