Python Tuples & Sets Explained - Unpacking, Set Operations & frozenset (Beginner Tutorial #9)
Video: Python Tuples & Sets Explained - Unpacking, Set Operations & frozenset (Beginner Tutorial #9) by Taught by Celeste AI - AI Coding Coach
Python Tuples & Sets
Tuples:
(1, 2, 3)— immutable, ordered. Sets:{1, 2, 3}— mutable, unordered, unique. Both fixed-size in different ways. Used for "this won't change" and "no duplicates."
After lists, the next two container types: tuples (read-only sequences) and sets (collections of unique items).
Tuples
colors = ("red", "green", "blue")
print(colors) # ('red', 'green', 'blue')
print(colors[0]) # 'red'
print(colors[-1]) # 'blue'
Tuples look like lists with parentheses. Indexing works the same.
The differences:
- Immutable —
colors[0] = "yellow"errors. - No
.append,.pop,.remove,.sort— all mutating methods absent. - Slightly faster than lists — fixed-size storage.
- Hashable (if all elements are) — can be dict keys or set members.
Single-element tuples
single = ("hello",)
print(type(single)) # tuple
not_a_tuple = ("hello")
print(type(not_a_tuple)) # str — parens were just grouping!
The trailing comma is critical for one-element tuples. Without it, the parens are just grouping.
Tuple unpacking
point = (10, 20)
x, y = point
print(f"x={x}, y={y}") # x=10, y=20
# Swap variables
a, b = 1, 2
a, b = b, a
print(a, b) # 2 1
# Multiple return values (functions return tuples)
def divmod_op(a, b):
return a // b, a % b # implicitly a tuple
q, r = divmod_op(17, 5)
Tuple unpacking is the idiom for multiple return values, swapping, and parallel assignment.
For "ignore some values":
_, important, _ = (1, 2, 3) # underscore = "I don't care"
first, *rest = [1, 2, 3, 4] # first = 1, rest = [2, 3, 4]
*init, last = [1, 2, 3, 4] # init = [1, 2, 3], last = 4
* captures the "rest" — useful for variable-length unpacking.
When to use tuples
- Fixed-size records.
(name, age),(x, y). Use adataclassorNamedTuplefor many fields. - Function returning multiple values. Return a tuple; caller unpacks.
- Dict keys, set members. Lists aren't hashable; tuples are (if all elements are).
- Values that mustn't change. Constants, configurations.
Tuple methods
scores = (90, 85, 90, 70, 90)
print(scores.count(90)) # 3
print(scores.index(85)) # 1
Just two methods (because immutable): .count(x) and .index(x).
Sets
unique = {1, 2, 3, 4, 4, 4} # duplicates auto-removed
print(unique) # {1, 2, 3, 4}
empty = set() # NOT {} — that's an empty dict
empty.add(1)
empty.add(2)
empty.add(1) # already there — no effect
print(empty) # {1, 2}
Sets are mutable, unordered, unique. Curly braces with comma-separated values (or set() for empty).
{} is an empty dict, not set. Use set() for empty set.
Set operations
a = {1, 2, 3}
b = {2, 3, 4}
print(a | b) # union: {1, 2, 3, 4}
print(a & b) # intersection: {2, 3}
print(a - b) # difference: {1}
print(a ^ b) # symmetric difference: {1, 4}
# Method form (same):
print(a.union(b))
print(a.intersection(b))
print(a.difference(b))
print(a.symmetric_difference(b))
Mathematical set operations. |, &, -, ^ are the operator forms.
add, remove, discard
s = {1, 2, 3}
s.add(4) # adds 4
s.add(2) # no effect (already in)
s.remove(3) # removes 3
s.remove(99) # KeyError — 99 not in set
s.discard(99) # no error if missing — safer
.add(x) adds; .remove(x) errors if missing; .discard(x) is silent if missing.
Membership: O(1)
big_set = set(range(1_000_000))
print(999999 in big_set) # ~instant
Set membership is O(1) (hash table). Compared to in on a list (O(n)), sets are dramatically faster for "is x in here" tests.
For "remove duplicates from a list":
nums = [1, 2, 3, 2, 1, 4]
unique = list(set(nums)) # order not preserved
unique = list(dict.fromkeys(nums)) # order preserved (Python 3.7+)
Set removes dupes but doesn't preserve order. dict.fromkeys does both.
frozenset
fs = frozenset([1, 2, 3])
fs.add(4) # ERROR — frozenset is immutable
print(fs) # frozenset({1, 2, 3})
# frozenset is hashable — usable as dict key
group_membership = {
frozenset({"alice", "bob"}): "admins",
frozenset({"carol"}): "users",
}
frozenset is the immutable cousin of set. Hashable — can be a dict key or set member.
When to use sets
- Membership testing on large data. O(1) vs O(n) for lists.
- Deduplication.
list(set(items)). - Set math. Union/intersection/difference of groups.
- Finding unique elements. Build a set as you walk; check
in.
Sets aren't ordered
s = {3, 1, 2}
print(s) # may print {1, 2, 3} or {3, 1, 2} or anything
Internal order is implementation-defined. Don't rely on it.
For an ordered set, no built-in — use dict.fromkeys(items).keys() or a third-party library.
A class roster example
students = [] # list — order matters, may have dupes
names = set() # set — O(1) "have we seen this name?"
def enroll(name):
if name in names:
print(f"{name} already enrolled")
return
students.append(name)
names.add(name)
Two parallel data structures: list for ordering, set for fast deduplication.
For a single structure with uniqueness and order, use a dict (lesson 10) — Python 3.7+ dicts preserve insertion order.
Common stumbles
Single-element tuple without comma. ("hello") is a string. ("hello",) is a 1-tuple.
{} for empty set. It's an empty dict. Use set().
Mutable items in a set. {[1, 2]} errors — lists aren't hashable. Use tuples.
Sets aren't ordered. Don't rely on iteration order.
Set item access. No s[0] — sets are unordered, no index. Iterate with for x in s.
Tuple "modification." Can't change. Can rebuild: t = t[:1] + (99,) + t[2:] produces a new tuple.
What's next
Lesson 10: dictionaries. Key-value mappings — Python's most powerful data structure.
Recap
Tuple: (1, 2, 3) — immutable, ordered. Hashable. Used for fixed records, multiple returns, dict keys. Single element needs trailing comma (x,). Two methods: .count, .index. Set: {1, 2, 3} — mutable, unordered, unique. O(1) membership. Operations: | & - ^. set() for empty (not {} — that's dict). frozenset is the immutable cousin (hashable). Use sets for dedup and fast membership; tuples for "fixed-size record."
Next lesson: dictionaries.