Sets
Sets
Sometimes you only want unique values.
For example:
- Unique tags
- Unique usernames
- Unique numbers
- Unique categories
If the same value appears many times, you may want to keep only one copy.
Python gives us sets for this.
What Is a Set?
A set is a collection of unique values.
This means duplicate values are removed automatically.
Sets use curly brackets {}.
numbers = {1, 2, 2, 3, 3}
print(numbers)
Output
{1, 2, 3}
Why Sets Matter
Sets help you:
- Remove duplicates
- Check values quickly
- Compare groups of data
- Store unique items
Creating a Set
colors = {"Red", "Blue", "Green"}
print(colors)
Empty Set
Use set().
items = set()
print(items)
Output
set()
Adding One Item
Use add().
colors = {"Red", "Blue"}
colors.add("Green")
print(colors)
Output
{'Red', 'Blue', 'Green'}
Adding Many Items
Use update().
colors = {"Red"}
colors.update(["Blue", "Green"])
print(colors)
Output
{'Red', 'Blue', 'Green'}
Removing with remove()
Removes an item.
colors = {"Red", "Blue", "Green"}
colors.remove("Blue")
print(colors)
Output
{'Red', 'Green'}
Safe Remove with discard()
Does nothing if item is missing.
colors = {"Red", "Green"}
colors.discard("Blue")
print(colors)
Output
{'Red', 'Green'}
Remove Random Item with pop()
Sets are unordered, so pop() removes any item.
colors = {"Red", "Blue", "Green"}
colors.pop()
print(colors)
Output
Output may be different each time
Clear All Items
colors.clear()
print(colors)
Output
set()
Copy a Set
Use copy().
a = {1, 2, 3}
b = a.copy()
print(b)
Output
{1, 2, 3}
Check If Item Exists
Use in.
colors = {"Red", "Blue"}
print("Red" in colors)
print("Green" in colors)
Output
True
False
Length of a Set
Use len().
colors = {"Red", "Blue", "Green"}
print(len(colors))
Output
3
Loop Through a Set
colors = {"Red", "Blue", "Green"}
for color in colors:
print(color)
Note
Set order is not guaranteed.
The printed order may change.
Set Operations
Use:
a = {1, 2, 3}
b = {3, 4, 5}
Union
All unique items from both sets.
print(a | b)
Output
{1, 2, 3, 4, 5}
Intersection
Only shared items.
print(a & b)
Output
{3}
Difference
Items in first set only.
print(a - b)
Output
{1, 2}
Symmetric Difference
Items in one set or the other, but not both.
print(a ^ b)
Output
{1, 2, 4, 5}
Subset
Checks if all items in one set exist in another.
small = {1, 2}
big = {1, 2, 3}
print(small.issubset(big))
Output
True
Superset
Checks if a set contains all items of another set.
print(big.issuperset(small))
Output
True
Disjoint
Checks if two sets share no items.
x = {1, 2}
y = {4, 5}
print(x.isdisjoint(y))
Output
True
Code Along
tags = {"python", "coding", "python"}
tags.add("course")
for tag in tags:
print(tag)
Mini Challenge
Build a unique skills manager.
Steps:
- Create a set with:
Python,HTML,Python - Print the set
- Add
CSS - Remove
HTML - Check if
Pythonexists - Print the union of your set with:
{"Git", "CSS"} - Print the final set
Expected output:
{'Python', 'HTML'}
True
{'Python', 'CSS', 'Git'}
{'Python', 'CSS'}
Real World Use Case
Programs use sets for tags, unique users, duplicate removal, permissions, and comparing groups of data.
Quiz
- What is a set?
- What happens to duplicate values?
- What does
add()do? - What does intersection return?
- What does symmetric difference return?
Assignment
Create two sets of favorite foods. Print the union, intersection, and difference.
Summary
You learned that sets store unique values and how to add, remove, copy, compare, and perform powerful set operations in Python.