ProgrammingHardCognizant GenC · TCS NQT

What is printed?

What is printed?
from collections import OrderedDict
od = OrderedDict()
for k in ['a', 'b', 'c']:
    od[k] = ord(k)
od.move_to_end('a')
od['b'] = 0
od.popitem(last=False)
print(list(od.items()))
  1. A. [('c', 99), ('a', 97)]
  2. B. [('a', 97), ('c', 99)]
  3. C. [('b', 0), ('c', 99), ('a', 97)]
  4. D. [('c', 99), ('a', 97), ('b', 0)]

Correct answer: A. [('c', 99), ('a', 97)]

move_to_end moves a to the back, and assigning to an existing key keeps its place. popitem(last=False) removes the oldest key, b, which leaves c and then a.

More Python Programming questions

Exams that ask this topic