Dictionaries & Sets · Question 8 · Medium

A key is added and an existing key is updated before the keys are listed. What is printed?

d = {"b": 2, "a": 1}
d["c"] = 3
d["b"] = 9
print(list(d))
  1. ['a', 'b', 'c']
  2. ['a', 'c', 'b']
  3. ['b', 'a', 'c', 'b']
  4. ['b', 'a', 'c']
Answer and explanation

Answer: D. ['b', 'a', 'c']

Explanation: list(d) gives the keys in insertion order: b, a, then the new key c. Updating the value of b does not move it or add a second copy.

Previous: From Python 3.7 onwards, in what order does a for loop go through the keys of… · Next: setdefault() is called twice with the same key. What does the dictionary look…

Practise dictionaries & sets

Try a fresh set of dictionaries & sets questions with an explanation after every answer, free and without signing up: Dictionaries & Sets practice. Or sit a full placement mock test.

More dictionaries & sets questions

More python practice

Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge