ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import bisect
a = [(1, 'x'), (4, 'y')]
bisect.insort(a, (3, 'z'), key=lambda t: t[0])
print(a)
  1. A. [(3, 'z'), (1, 'x'), (4, 'y')]
  2. B. [(1, 'x'), (3, 'z'), (4, 'y')]
  3. C. [(1, 'x'), (4, 'y'), (3, 'z')]
  4. D. [(1, 'x'), (3, 'z'), (4, 'y'), (3, 'z')]

Correct answer: B. [(1, 'x'), (3, 'z'), (4, 'y')]

insort with key inserts a whole element at the position found by comparing key values. The key is not applied to the value being inserted, so you must pass the element, here the tuple (3, 'z').

More Python Programming questions

Exams that ask this topic