ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: D. [('b', 2), ('a', 4)]

Updating an existing key keeps its position, but deleting and reinserting a key moves it to the end. Here 'a' is removed and added again after 'b'.

More Python Programming questions

Exams that ask this topic