C
Python/Intro/Lesson 08

set — A Collection Without Duplicates

15 min·theory
This chapter
7/7

set — A Collection Without Duplicates

🎯 After Reading This Lesson

Once you finish this lesson, you will be able to confidently do the following 3 things.

  • ✅ Use set for duplicate removal + set operations (& | -)
  • ✅ Know when to convert between list and set
  • ✅ Understand the hashable requirement (for set/dict keys)

Keep these goals as a checklist, and close the lesson once you can answer all of them.

8 set Patterns — Code + Output

set = no order · no duplicates · hash-based. Membership check O(1) — 100x faster than a list. Mirrors mathematical set operations directly.


1. Creating a Set

python
numbers = {1, 2, 3, 4, 5}              # Curly braces + values
names = {"Hong Gil-dong", "Lee Mong-ryong"}

# list → set (duplicates automatically removed)
duplicates_exist = [1, 2, 2, 3, 3, 3]
no_duplicates = set(duplicates_exist)
print(no_duplicates)                       # {1, 2, 3}

# ⚠️ An empty set is not {} (that's a dict)
empty_set = set()                        # ← Like this
empty_dict = {}                          # This is a dict

2. Adding and Removing

python
s = {1, 2, 3}

s.add(4)                # {1, 2, 3, 4}
s.add(2)                # Already exists → ignored (no error)

s.remove(3)             # {1, 2, 4}
# s.remove(99)          # KeyError — error if not found

s.discard(99)           # No error if not found (safe)

popped_value = s.pop()        # Remove + return an arbitrary element

add / remove / discard / pop — similar to list, but no order.


3. in — O(1) Membership Check (the real power of set)

python
large_list = list(range(1_000_000))
large_set  = set(range(1_000_000))

# list search — O(n), approx 0.5s for 1 million checks
print(999_999 in large_list)

# set search — O(1), immediate (microseconds)
print(999_999 in large_set)

If you need to frequently check whether a value exists, always use a set.


4. Set Operations — Just Like Math

python
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

# Union (present in at least one)
print(A | B)            # {1, 2, 3, 4, 5, 6}
print(A.union(B))       # Same result

# Intersection (present in both)
print(A & B)            # {3, 4}
print(A.intersection(B))

# Difference (present in A but not in B)
print(A - B)            # {1, 2}

# Symmetric difference (present in only one)
print(A ^ B)            # {1, 2, 5, 6}

5. Subset and Superset

python
small = {1, 2}
large   = {1, 2, 3, 4}

print(small <= large)        # True (small ⊆ large)
print(small < large)         # True (proper subset)
print(large >= small)        # True

# Check for no overlapping elements
A = {1, 2, 3}
B = {4, 5, 6}
print(A.isdisjoint(B))   # True

6. Set Comprehension

python
text = "Hello World"
char_set = {c.lower() for c in text if c.isalpha()}
print(char_set)              # {'h', 'e', 'l', 'o', 'w', 'r', 'd'}

# Even squares
even_squares = {x*x for x in range(10) if x % 2 == 0}
print(even_squares)             # {0, 4, 16, 36, 64}

7. Practical Pattern — Removing Duplicates

python
# Remove duplicates from a list (order not preserved)
data = [1, 2, 2, 3, 3, 3, 4, 1]
unique = list(set(data))
print(unique)                  # [1, 2, 3, 4]  (order irrelevant)

# Common elements of two lists
A_elements = [1, 2, 3, 4, 5]
B_elements = [3, 4, 5, 6, 7]
common = list(set(A_elements) & set(B_elements))
print(common)                  # [3, 4, 5]

# Check if only specific characters are present
required_chars = set("abcde")
input_text = "alphabet"
present_chars = required_chars & set(input_text)
print(present_chars)               # {'a', 'b', 'e'}

8. frozenset — Immutable Set

python
fs = frozenset([1, 2, 3])

# fs.add(4)              # AttributeError — immutable

# True value — can be dict key·set element
path_cache = {
    frozenset(["A", "B"]): "straight",
    frozenset(["A", "B", "C"]): "triangle",
}
print(path_cache[frozenset(["B", "A"])])    # straight (order irrelevant)

A regular set cannot be used as a dict key, but a frozenset can because it is immutable.


list vs set vs dict — When to Use Which

listsetdict
Order✅ (3.7+)
Duplicateskey X (value O)
SearchO(n)O(1)O(1)
Indexing[0]["key"]
Use caseOrdered dataMembership / dedupName-to-value mapping

One-Line Summary

OperationCode
Create{1, 2, 3} or set()
Adds.add(x)
Removes.discard(x) (safe)
Checkx in s (O(1))
UnionA \B
IntersectionA & B
Deduplicatelist(set(lst))

🐍 Try It Out — set — Run It Yourself

Run the concepts above as actual code. The fastest way to learn is to change the values and observe what happens yourself.
✏️ Python 코드
📟 Console output
▶ Press the Run button
🐍 Real Python via Pyodide — first run takes 3–5s to load

🤖 Try Asking AI Like This

Knowing the concepts in this lesson lets you give AI specific instructions. Instead of a vague 'fix this,' you make vocabulary-driven requests — that's where token savings start.

  • 'Replace the duplicate removal in this list with a set'
  • 'Rewrite this dict merge using dict | dict (Python 3.9+)'

Why This Reduces Tokens

Without the vocabulary, even after receiving an AI answer you have to ask 'What does that mean?' again. That follow-up question is what burns tokens. Learn the concept once and the conversation ends in one round.

set — A Collection Without Duplicates - Python