Ordering & Aliasing

Preview — 3 of 10 questions

What is the output of the following code?

javascript
from collections import OrderedDict

d1 = OrderedDict([("a", 1), ("b", 2)])
d2 = OrderedDict([("b", 2), ("a", 1)])
regular1 = {"a": 1, "b": 2}
regular2 = {"b": 2, "a": 1}

print(d1 == d2)
print(regular1 == regular2)
AFalse, True
BTrue, False
CFalse, False
DTrue, True

What is the output of the following code?

javascript
from collections import ChainMap

defaults = {"color": "blue", "size": "M"}
user_prefs = {"color": "red"}

combined = ChainMap(user_prefs, defaults)

print(combined["color"])
print(combined["size"])
user_prefs["size"] = "L"
print(combined["size"])
Ablue, M, L
BTypeError
Cred, M, M
Dred, M, L

What is the output of the following code?

javascript
pairs = [("a", 1), ("b", 2), ("a", 3)]
result = {k: v for k, v in pairs}
print(result)
AKeyError: duplicate key 'a'
B{'a': 3, 'b': 2}
C{'a': 1, 'b': 2}
D{'a': [1, 3], 'b': [2]}

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.