Dictionaries and Sets

Lesson 8 of 20

Store data under meaningful keys and collect unique items — the two structures Python reaches for daily.

The big picture

Look things up by name

Dictionaries map keys to values for instant lookups; sets store each unique item exactly once.

Dict = Key to Value

{"Ada": 36} stores a pair; ages["Ada"] fetches the value in one hop.

Keys Are Unique

Writing to an existing key overwrites it; a brand-new key is simply added.

in Checks Membership

"Ben" in ages asks if the key exists — sets answer the same question for their items.

Sets Deduplicate

{1, 2, 2, 3} collapses to {1, 2, 3} — duplicates are impossible in a set.