ProgrammingModerateCognizant GenC · TCS NQT

What is printed?

What is printed?
d = {'a': 1, 'b': 2, 'c': 3}
d['a'] = 9
del d['b']
d['b'] = 5
print(list(d.items()))
  1. A. [('a', 9), ('b', 5), ('c', 3)]
  2. B. [('b', 5), ('a', 9), ('c', 3)]
  3. C. [('a', 9), ('c', 3), ('b', 5)]
  4. D. [('a', 1), ('c', 3), ('b', 5)]

Correct answer: C. [('a', 9), ('c', 3), ('b', 5)]

Updating an existing key keeps its position, deleting b removes it, and reinserting b puts it at the end. The insertion order is then a, c, b.

More Python Programming questions

Exams that ask this topic