← Quiz: The collections Module: all 10 questions
Quiz: The collections Module · Question 3 of 10
What does this print? (code)
What does this print?
from collections import defaultdict
d = defaultdict(int)
for ch in "aab":
d[ch] += 1
print(dict(d))
- A.{'a': 1, 'b': 1}
- B.{'a': 2, 'b': 1}
- C.An error
- D.{'aab': 1}
Answer & explanation
Correct answer: {'a': 2, 'b': 1}
int() gives 0 for each new key, which is then increased.
Can you get the rest right without peeking?
Take the timed Quiz: The collections Module quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →