Python for Data Analytics • Lesson 10
Sets
Learn how Python sets store unique values and how to add, remove, compare and analyze collections of data.
0 of 8 concepts completed
Complete the activity and related MCQ to unlock the next concept.
What Is a Set?
A set is a collection of unique values. Order is not preserved, and duplicate values are silently discarded. Sets are written with curly braces { } when they contain values.
Learn by Seeing
Unique values, unordered
numbers = {10, 20, 30}
print(numbers)
# {10, 20, 30} (order may vary)A set always contains each value at most once.
Edit the values — see duplicates collapse into unique entries:
list input
as a set
Interactive Activity
How many values are stored in numbers = {10, 20, 20, 30}?
Hint: Duplicates are dropped, so only unique values remain.
Python Analyst Mindset
Unique values and set algebra
"How many distinct customers?" "Which products appear in both months?" "Which regions are missing from this dataset?" — these are all set questions. Sets give you deduplication and comparison in a single line.
Deduplicate
set(data) removes repeats.
Test membership
in is fast and Boolean.
Compare
| & - for group comparisons.
Analyze
Count unique categories in one line.
Frequently Asked Questions
Sets — FAQs
Python reserves for empty dictionaries. Use set() when you need an empty set.
remove() raises a KeyError when the element is missing. discard() does nothing.
Sets are unordered, so no. Convert to a list first (sorted(my_set)) if you need order.
| is union — every distinct element from both sets, with duplicates removed.
Lesson Summary
What you learned
A set keeps one copy of each element.
creates a dictionary; set() creates an empty set.
add inserts; discard removes safely.
pop removes an arbitrary element; in tests membership.
Everything from both sets.
Shared values, and values unique to the left set.