🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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"])
  1. A.['Asha', 'Neha'] KeyError
  2. B.['Asha', 'Neha'] None
  3. C.['Asha', 'Neha'] []
  4. 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 →