ProgrammingModerateInfosys · Python

What is printed?

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

Correct answer: B. [('b', 2), ('c', 3), ('a', 9)]

Dicts keep insertion order; deleting 'a' and inserting it again places it at the end.

More Python Programming questions

Exams that ask this topic