ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(sorted(['x10', 'x9', 'x1'], key=lambda s: int(s[1:])))
  1. A. ['x1', 'x10', 'x9']
  2. B. ['x1', 'x9', 'x10']
  3. C. ['x10', 'x1', 'x9']
  4. D. ['x9', 'x1', 'x10']

Correct answer: B. ['x1', 'x9', 'x10']

The key takes the number after the x, so the values are 10, 9 and 1. Sorting those numbers numerically gives x1, x9 and x10.

More Python Programming questions

Exams that ask this topic