Embercode
✕
Dicts and SetsQuick quiz

Map and set bug hunt

8 questions. Covers Debugging lab: dict and set pitfalls in Python.

Questions 1 to 5 of 8

  1. 1. The author wants to count the tags for doc2 without changing the dict. What is the bug in this code? ``python from collections import defaultdict tags = defaultdict(list) tags['doc1'] = ['python'] n = len(tags['doc2']) ``
  2. 2. The author runs this code with no score for bo. What goes wrong? ``python score = {'ana': 5} bo = score['bo'] print('bo: ' + str(bo)) ``
  3. 3. The author wants to remove every key whose value is 0. What is the bug in this loop? ``python for item in stock: if stock[item] == 0: del stock[item] ``
  4. 4. The answers this code prints are correct, but the code has a bug. What is it? ``python from collections import defaultdict friends = defaultdict(set) friends['ana'] = {'bo', 'cy'} for name, other in [('ana', 'bo'), ('dev', 'ana')]: print(other in friends[name]) ``
  5. 5. What does this code print? ``python m = {'kiwi': 2} m['pear'] = 0 m['fig'] = 0 m['lime'] = 1 m['pear'] = 4 m = {k: v for k, v in m.items() if v != 0} print(dict(sorted(m.items()))) ``

5 question(s) left on this page.