๐Ÿงฎ
๐Ÿงฎ Data Structures & Algorithms ยท DS-101 ยท Session 3 of 6

Dictionaries & Sets: Instant Lookup

Dictionaries and sets are the secret weapons of fast code. Thanks to hashing, they find things almost instantly โ€” turning slow scans into direct lookups.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

If lists are the workhorse, dictionaries and sets are the secret weapons. Both use a technique called hashing to find items almost instantly, no matter how much data you have - the opposite of a list's slow scan. Learning to reach for a dict or set at the right moment is one of the most practical skills in this whole course, and it makes a huge difference to real code.

๐Ÿ“Œ Worked example
Minutes to milliseconds. A beginner's script checks each of 100,000 incoming ids against a list of 100,000 known ids to find new ones - and it takes ages, because every check scans the whole list (100,000 x 100,000 comparisons). Changing the known ids from a list to a set - one word - makes each check near-instant, and the whole script finishes in a blink. Nothing about the logic changed; the data just moved into a structure built for fast lookup. That single-structure swap is one of the most common and dramatic speedups beginners discover.

Hashing = near-instant lookup

A dictionary maps keys to values, and a set holds unique items - and both can check 'is this here?' or 'what's the value for this key?' in roughly constant time, however big they get.

StructureForLookup speed
dict {key: value}Look up a value by its keyNear-instant
set {items}Unique items + fast membershipNear-instant
list [items]Ordered data (by contrast)Slow scan to find

The magic is hashing: the structure computes where an item should live from the item itself, so it can jump straight there instead of searching. That's why 'x in myset' is fast even for millions of items.

When to reach for them

Use a dict when you look data up by a name/key; use a set when you need uniqueness or fast membership checks.

# count words with a dict (key -> count)
counts = {}
for word in text.split():
    counts[word] = counts.get(word, 0) + 1

# fast membership + de-duplication with a set
seen = set()
for item in stream:
    if item in seen:      # near-instant, no scan
        continue
    seen.add(item)
๐ŸŽฏ
Slow 'x in list'? Reach for a set or dict. The single most common real-world speed win for beginners is replacing repeated membership checks against a list with a set, or repeated 'find the thing with this id' scans with a dict keyed by id. It's a small change that can make code dramatically faster. Whenever you catch yourself scanning a collection to find things by value or key, that's the signal to use a set or dict instead.

Look things up fast

  • Dict - map keys to values; look up by key near-instantly.
  • Set - unique items with fast membership and easy de-duplication.
  • Hashing is why both are fast - they compute where data lives.
  • Replace list scans with a set/dict whenever you look things up by value/key.

๐ŸŽฏ Key takeaways

  • Dictionaries (key->value) and sets (unique items) use hashing for near-instant lookup, however big.
  • This is the opposite of a list's slow scan - the magic is computing where an item lives.
  • Use a dict to look up by key (like counting words); use a set for uniqueness and fast membership.
  • The top beginner speed win: replace repeated 'x in list' scans with a set or dict.

Frequently asked

What is 'hashing' and why does it make lookups so fast?

Hashing is computing a number (a 'hash') from an item, which the structure uses to decide where to store it. The clever part: because the location is derived from the item itself, when you later ask 'is this item here?' or 'what's the value for this key?', the structure recomputes the hash and jumps straight to where it would be - instead of searching through everything. That's why a set or dictionary can check membership or fetch a value in roughly constant time no matter how many items it holds, while a list has to scan. You don't need to understand the maths of hash functions to use them; just know that dicts and sets 'know where to look' thanks to hashing, which is what makes them fast.

If sets and dicts are so fast, why not always use them instead of lists?

Because they trade away things lists provide. Sets and dictionaries don't give you position-based access (there's no 'the third item' in a set the way there is in a list), sets don't allow duplicates, and while modern Python dicts preserve insertion order, sets are conceptually unordered. Lists are the right choice when order and position matter, when you need duplicates, or when you're iterating in sequence. The point isn't that one structure is best - it's matching the structure to the job: lists for ordered/positional data, sets for uniqueness and membership, dicts for key-based lookup. Great programmers use all three, each where it fits, and often combine them.

Session 3 of 6 complete

Next up: Stacks, Queues & Beyond

Continue to Session 4 โ†’

Related lessons

More in this course

Course home → · Browse all courses →