Fill in

Dict & Set Comprehensions

The same trick works with curly braces:

names = ["moss", "ash", "brine"]

lengths = {n: len(n) for n in names}      # dict: key: value
# {'moss': 4, 'ash': 3, 'brine': 5}

firsts = {n[0] for n in names}           # set: just values
# {'m', 'a', 'b'}

To transform an existing dict, loop over .items() to get each key and value together:

stock = {"rope": 2, "lamp": 0}
flipped = {count: item for item, count in stock.items()}
# {2: 'rope', 0: 'lamp'}

Pip tip: {} on its own is an empty dict. An empty set is written set().

Assignment

Replace each ___ so that:

  1. doubled has the same items with every price doubled.
  2. pricey keeps only items costing at least 10.
  3. letters is the set of first letters of every item name.