Embercode
✕
Built-in CollectionsQuick quiz

Grouping with dicts

6 questions. Covers Grouping with dicts of lists.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``python words = ['kiwi', 'fig', 'plum', 'apple', 'date'] groups = {} for w in words: groups.setdefault(len(w), []).append(w) print(sorted(groups.items()), groups.get(4)) ``
  2. 2. Which line adds a record to its key's list the first time the key appears and on every later time, without losing the records already added?
  3. 3. What does this program print? ``python orders = {} rows = [('ana', 'book'), ('ana', 'lamp')] for name, item in rows: orders[name] = [item] print(orders) ``
  4. 4. Which dict method returns the value for a key, or stores a default value under that key and returns it when the key is missing? Give the method name.

    Type your answer.

  5. 5. If you call get on a key that is not in the dict, then call append on the result, which exception does Python raise? Give its name.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.