Skip to content

The basics · Dictionaries

Dictionaries

Look values up by a key instead of by position. · 10 minutes

A dictionary maps keys to values: ages = {"ada": 36, "alan": 41}. ages["ada"] is 36; ages["grace"] = 85 adds a pair; "ada" in ages asks whether the key is there. ages.get("bob", 0) gives 0 instead of an error when the key is missing.

Predict first

What happens on the last line?

1counts = {"apple": 2}
2counts["pear"] = counts["pear"] + 1