
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 writtenset().
Assignment
Replace each ___ so that:
doubledhas the same items with every price doubled.priceykeeps only items costing at least 10.lettersis the set of first letters of every item name.
Replace each ___ so that:
doubledhas the same items with every price doubled.priceykeeps only items costing at least 10.lettersis the set of first letters of every item name.
main.py Python sleeping
Press Run to try your code, then Check to test it.