Sets in Python#
đ Data Analysis Using Python đ Strings & Data Structures Lesson 025
â Previous ¡ Next ✠¡ â Section ¡ â Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Unordered collections of unique values#
The last core data structure is the set â an unordered collection of unique values. Sets excel at membership testing, removing duplicates, and set operations (union, intersection), and understanding them completes Pythonâs built-in structures. This lesson covers sets, closing the structures stage.
What a set is#
A set is a collection with two defining properties: its values are unique (no duplicates) and unordered (no positional index). Written with curly braces (like a dictionary, but values not pairs):
regions = {"North", "South", "East", "North"} # duplicate ignored
print(regions) # {"North", "South", "East"} â 3 unique values
Adding a duplicate has no effect (the value is already present), and there is no
set[0] â sets are not indexed. Their purpose is membership and uniqueness, not
order.
Set operations#
Sets support fast membership testing and mathematical set operations:
"North" in regions # True â fast membership test
a = {1, 2, 3}
b = {2, 3, 4}
a | b # {1, 2, 3, 4} â union (in either)
a & b # {2, 3} â intersection (in both)
a - b # {1} â difference (in a but not b)
Membership testing (in) is very fast on a set â faster than searching a list â and the
operations (union |, intersection &, difference -) answer âin either / both /
one but not the otherâ directly. These are exactly the set operations of mathematics, and of
SQLâs UNION/INTERSECT/EXCEPT.
Why sets matter#
Sets serve specific, common needs:
Removing duplicates â converting a list to a set drops duplicates instantly (
set(my_list)), the fastest deduplication in Python.Fast membership testing â checking whether a value is in a large collection is far faster with a set than a list, which matters at scale.
Comparing collections â the set operations answer âwhat is common / different between these two collections?â directly (which customers are in both lists? which are only in one?).
For these tasks â uniqueness, membership, comparison â the set is the right tool, cleaner and faster than working around a list.
The caveat#
Setsâ properties are also their limitations. Being unordered, a set cannot be indexed or sliced, and does not preserve insertion order â if order matters, a set is the wrong structure (use a list). Being unique, a set cannot hold duplicates â which is the point for deduplication, but means a set cannot represent data where repetition is meaningful (a set of sales figures would collapse identical amounts into one, losing information). And set elements must be immutable (like dictionary keys), so a set cannot contain lists. Use a set precisely when uniqueness and membership are what you want, and a list when order or repetition matters. This completes Pythonâs core data structures; the next lessons open the final stage â the libraries that make Python a data-analysis powerhouse.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/sets-in-python/ (insightful-data-lab.com).