1. Concept library
  2. Python Foundations
  3. Module 5: Dictionaries and sets

Concept

Sets and uniqueness

A set holds each value once and answers "is this in here?" instantly. Union, intersection and difference compare whole collections in one expression.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what a set keeps and why membership checks are fast
  2. Use union, intersection and difference to compare two collections
  3. Write code that removes duplicates from a list with a set

In practice

A few lines that show Sets and uniqueness doing its job.

Python
seen = {"ada", "grace"}
seen.add("ada")              # no change
print(len(seen))             # 2

a = {1, 2, 3}
b = {3, 4}
print(a & b, a | b, a - b)   # {3} {1, 2, 3, 4} {1, 2}
unique = set([3, 1, 3, 2])

The common mistake

Watch for

Writing empty = {} to make an empty set. Those braces make a dict. An empty set is set().

What to know first

Your roadmap puts these before Sets and uniqueness, unless you already know them.

How it stays learned

Once a session shows you understand Sets and uniqueness, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Iterating over dictionaries