Python Tuples and Sets
Learn when to use immutable tuples and unique-value sets, how packing and unpacking work, and how set operations answer membership and collection questions.
Basic
Lists and dictionaries are useful when you need mutable collections. Python also gives you two different shapes for different questions:
tuple is an immutable sequence, while
set is a collection of distinct hashable elements. They are not smaller versions of lists and dictionaries; their rules make different operations natural.
Tuples: a sequence that does not change
A tuple keeps elements in order and supports sequence operations such as indexing and unpacking, but the tuple itself cannot be changed after it is created. That makes a tuple useful when the collection is meant to stay together as one value.
point = (40.7128, -74.0060)
print(point[0])
print(point[1])A tuple preserves the order of its elements and supports indexing.
Tuple syntax is driven by commas, not by parentheses alone. The comma is what makes
1, 2 a tuple. Parentheses are often used for readability or to make grouping unambiguous.
empty = ()
singleton = ("Python",)
pair = "Python", "3.14"
print(empty)
print(singleton)
print(pair)The comma creates tuple packing; a singleton needs a trailing comma.
Packing and unpacking
When several values are separated by commas, Python can pack them into a tuple. The reverse operation is sequence unpacking: several names receive the corresponding elements from a sequence. The number of targets must match unless extended unpacking is used.
version = (3, 14, 8)
major, minor, patch = version
print(major)
print(minor)
print(patch)Packing and unpacking let one sequence carry several related values.
Sets: unique values, not positions
A set answers membership and combination questions. It contains distinct hashable elements and does not provide sequence indexing or slicing. If the same value is added more than once, the set still contains one element.
languages = {"Python", "JavaScript", "Python", "CSS"}
print(sorted(languages))
print("Python" in languages)
print("Ruby" in languages)The duplicate value disappears, while membership remains a direct set operation. Sorting here is only to make the displayed output deterministic.
An empty set is written with
set() because
{} creates an empty dictionary. Non-empty sets can use braces with comma-separated elements, or the
set() constructor with an iterable.
Set operations describe relationships
Set operations are useful when the question is about overlap or difference rather than position. Union combines elements, intersection keeps common elements, difference removes elements found in another set, and symmetric difference keeps elements that occur in exactly one of the sets.
frontend = {"HTML", "CSS", "JavaScript"}
backend = {"Python", "SQL", "JavaScript"}
print(sorted(frontend | backend))
print(sorted(frontend & backend))
print(sorted(frontend - backend))
print(sorted(frontend ^ backend))The four common set operations answer four different relationship questions.
Changing a set
Unlike tuples, ordinary sets are mutable.
add() inserts one element, while
remove() requires the element to be present and raises
KeyError when it is absent.
discard() removes an element when present and otherwise does nothing.
tags = {"python", "tutorial"}
tags.add("basics")
tags.discard("draft")
print(sorted(tags))Use set mutation when the set represents a changing collection of unique values.
frozenset: the immutable set
A
frozenset has set semantics but cannot be changed after creation. Because it is immutable and hashable, it can be used where a set itself needs to be an element of another set or a dictionary key. You do not need it merely because a set should be read-only; introduce it when hashable set values are actually useful.
roles = frozenset({"reader", "editor"})
permissions = {roles, frozenset({"reader"})}
print(sorted(roles))
print(len(permissions))A frozenset can be stored inside another set because it is hashable.
Tuple or set?
Choose a tuple when order matters and the grouped values should not be replaced through tuple operations.
Choose a set when uniqueness and membership or set relationships are the important questions.
Choose a frozenset when you need set semantics but the set itself must be immutable and hashable.
Do not choose a set merely because you want a collection: sets deliberately give up indexing and sequence order.
Run the complete example
This example combines the two container shapes without making them compete. The tuple records a fixed coordinate; the sets compare capabilities.
Output
Ready to run.
Before moving on, take the Python Tuples and Sets quiz and then complete the Tuples and Sets code exercise.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.