×

python · level 3 · unit 1 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Curly brackets make a dict or .

Swap the square brackets for curly ones and you can build a dictionary instead. A needs a key and a value with a colon between them: {name: len(name) for name in names}.

Loop over .items() to build one dictionary from another, for example turning pence into pounds, or keeping only the stock that is running low.

Leave out the colon and you get a set comprehension. Sets keep no repeats, so {tag.lower() for tag in tags} tells you which different tags appeared.

stock = {"apples": 40, "pears": 3, "plums": 0}
low = {fruit: count for fruit, count in stock.items() if count < 5}
tags = ["Python", "python", "SQL", "sql", "Go"]
unique = {tag.lower() for tag in tags}
print(low)
print(sorted(unique))

A set has no order, so sorted hands its items back as a list in order.