Python Programming · Foundations
Sets
On this page 9 sections
In 30 seconds
A Python set Python’s mutable unordered collection of distinct hashable objects. Full entry → is an unordered collection of distinct, hashable Able to provide a hash value that does not change during its lifetime; Python requires set members to be hashable. Full entry → objects. It is useful when the question is whether an item is present, when repeated values should collapse into one value, or when two groups need to be combined or compared. Use set when the collection must change; use frozenset when it must stay fixed. Sets do not provide positions, indexes, or slices.
Why this matters
Sets turn several common programming tasks into direct statements of intent: remove repeated values, test membership The relationship tested by asking whether an object is in a collection, such as with x in items. Full entry →, or find what two groups share. They also introduce an important design choice: mutability. A mutable set can be updated as a program runs, while an immutable frozenset can safely represent a fixed group and is hashable. Learning the distinction helps students select a collection for its behavior rather than for familiar punctuation.
The college version
What a set represents
In Python, a set is an unordered collection of distinct hashable objects. “Distinct” means that a value appears at most once in the collection. For example, {'red', 'blue', 'red'} describes a set with two members, not three: the repeated 'red' does not create another member An object contained in a set. Full entry →. This makes a set a good fit for a collection whose question is membership rather than count or position. The expression 'red' in colors asks directly whether the member is present. A set can also be made from another iterable, such as set(['red', 'blue', 'red']), which eliminates the repeated value.
“Unordered” is a behavioral constraint, not merely a warning about how output happens to look. A set does not record an insertion position and does not support indexing or slicing. Code should not treat a printed set as a stable ordered display, and colors[0] is not a valid way to obtain a first member. When a task needs a sequence position, a list or tuple is the appropriate kind of collection; this lesson concerns membership-oriented collections instead. The official documentation also specifies that set elements must be hashable. That requirement rules out putting a mutable set inside another set.
Creating and changing sets
A nonempty set literal uses braces with comma-separated members: {'red', 'blue'}. The constructor set(iterable) is particularly helpful when converting an existing iterable or when creating an empty set. Write set() for an empty set. The literal {} does not create an empty set; it creates an empty dictionary, a different collection type. This punctuation detail is worth learning because a program may otherwise run with the wrong kind of object.
The built-in set type is mutable: its contents can be changed. For example, colors.add('green') adds a member if it is not already present, and colors.remove('blue') removes a present member. Mutability is useful for accumulating the unique values seen while a program runs. It also means a set is not hashable. Python’s other built-in set type, frozenset, has the opposite update rule: after it is created, its contents cannot be altered. A frozenset is hashable, so it can be used where a fixed, hashable value is required, including as an element of another set. This is not a claim that one is universally better: choose set when updates are part of the job and frozenset when fixed membership is the job.
Set relationships and operations
Python exposes familiar set relationships through both methods and operators. If morning = {'Ada', 'Bo'} and afternoon = {'Bo', 'Cy'}, then morning | afternoon is the union A set operation that includes every member found in either input set. Full entry →: everyone appearing in either group. morning & afternoon is the intersection A set operation that includes only members shared by all input sets being compared. Full entry →: the members both groups share. morning - afternoon is the difference A set operation that includes members of one set that are absent from another specified set. Full entry →: members in the left-hand group but not the right-hand group. The corresponding methods are union, intersection, and difference. Each operation answers a different question, so substituting one for another can silently produce a plausible but wrong result.
For a concrete programming task, imagine a club wants to identify people who attended a morning meeting but missed an afternoon meeting. The relevant operation is morning - afternoon, not union and not intersection. With the values above, the result is {'Ada'}. If instead the club needs every person who attended either meeting, it uses union; if it needs people who attended both, it uses intersection. Since a set’s output order is not a stored sequence order, examples should compare set contents rather than depend on a particular printed arrangement. These operations return sets containing the appropriate members; they do not turn a set into a mapping or a sequence. Operators require set operands, whereas the named methods accept any iterable as an argument. In beginner code, using the method name can make the intended relationship easier to read; using an operator can be concise when both operands are visibly sets.

Eli explains
The same idea, in plain words
Explain it like I’m 10
Think of a set as a bowl that keeps one copy of each kind of bead. Put in two red beads and one blue bead, and the bowl’s inventory is still just red and blue. It is good for questions like “Do we have a red bead?” or “Which colors are in both bowls?” It is not good for “What is the first bead?” because the bowl does not label places 0, 1, and 2.
Picture it like this
Two bowls of colored beads can show the operations. Pour both bowls into a new bowl for a union. Keep only colors that occur in both bowls for an intersection. Start with the first bowl and remove colors also found in the second for a difference. A regular set is a bowl you may add to; a frozenset is sealed after it is made.
Where the picture stops working
Real bowls can contain two physically separate red beads and can be lined up in an order. A Python set represents distinct values, not physical objects or positions. It also has the technical hashability rule, which the bowl analogy does not show.
Worked example
Suppose morning = {'Ada', 'Bo', 'Ada'} and afternoon = {'Bo', 'Cy'} record attendance by name. First, morning has only {'Ada', 'Bo'} because the duplicate name does not create a second member. The expression 'Cy' in morning is False. For all attendees, compute morning | afternoon, whose members are Ada, Bo, and Cy. For attendees at both meetings, compute morning & afternoon, whose only member is Bo. For people at the morning meeting but not the afternoon meeting, compute morning - afternoon, whose only member is Ada. The printed order may differ, but these member relationships do not.
Key takeaway
Use a Python set for distinct, membership-focused values and choose the operation that matches the relationship you need. Use frozenset when the set must not change and needs to be hashable.
Quick check
3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.
What is true after executing colors = {'red', 'blue', 'red'}?
Given a = {'Ada', 'Bo'} and b = {'Bo', 'Cy'}, which expression produces the members in both sets?
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related
You’ll learn to
- Define a Python set and identify its defining properties.
- Create sets, including an empty set, and explain how duplicates are handled.
- Apply membership, union, intersection, and difference to a small problem.
- Distinguish mutable
setobjects from immutablefrozensetobjects. - Explain why indexing is not a set operation.
Common mistakes
Using
{}to make an empty set.Use
set();{}creates an empty dictionary.Expecting duplicate additions to preserve multiple copies.
A set represents distinct members, so equal duplicate values collapse to one member.
Indexing a set as if it had a first element.
Sets do not record positions and do not support indexing or slicing.
Choosing union when the task asks who is in both groups.
Use intersection (
&) for shared members; union (|) means either group.
Easily confused
set vs. frozenset
A set is mutable and unhashable; a frozenset is immutable and hashable.
union (`|`) vs. intersection (`&`)
Union includes members from either input; intersection includes only shared members.
difference (`a - b`) vs. intersection (`a & b`)
Difference keeps members unique to the left operand; intersection keeps members common to both.
Key vocabulary
- set
- Python’s mutable unordered collection of distinct hashable objects.
- frozenset
- Python’s immutable, hashable built-in type for a collection of distinct set members.
- member
- An object contained in a set.
- membership
- The relationship tested by asking whether an object is in a collection, such as with
x in items. - hashable
- Able to provide a hash value that does not change during its lifetime; Python requires set members to be hashable.
- union
- A set operation that includes every member found in either input set.
- intersection
- A set operation that includes only members shared by all input sets being compared.
- difference
- A set operation that includes members of one set that are absent from another specified set.
Sources & references
- Built-in Types — Python 3 documentation — Python Software Foundation
- 5. Data Structures — The Python Standard Library Documentation — Python Software Foundation
EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.
Researched 2026-08-19
Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.

