← Quiz: Modules and Libraries: all 14 questions
Quiz: Modules and Libraries · Question 11 of 14
What does this print? (code)
What does this print?
from collections import defaultdict
groups = defaultdict(list)
for name, branch in [("Asha", "CSE"), ("Ravi", "ECE"), ("Neha", "CSE")]:
groups[branch].append(name)
print(groups["CSE"], groups["IT"])
- A.['Asha', 'Neha'] KeyError
- B.['Asha', 'Neha'] None
- C.['Asha', 'Neha'] []
- D.['Neha'] []
Answer & explanation
Correct answer: ['Asha', 'Neha'] []
A defaultdict creates an empty list for a missing key instead of raising KeyError.
Can you get the rest right without peeking?
Take the timed Quiz: Modules and Libraries quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →