Lesson 34/48 ยท ๐ Real World Python
๐ Real World PythonLesson 34/48
Phase 7 ยท Real World Python15 min
Sets, Unique Collections
Automatically deduplicate data and test membership in O(1)
A set is an unordered collection with no duplicate values. It's perfect when you want unique items or need to quickly test whether a value exists.
Creating sets and deduplicationpython
# Create a set with curly braces (like a dict, but no key:value)
fruits = {"apple", "banana", "apple", "orange", "banana"}
print(fruits) # {'orange', 'banana', 'apple'} (order not guaranteed, no duplicates)
# Create from a list (great for deduplication)
numbers = [1, 2, 2, 3, 3, 3, 4]
unique = set(numbers)
print(unique) # {1, 2, 3, 4}Sets are unordered, you cannot index them with
[0]. If you need ordering, convert back to a list: sorted(my_set).Modifying sets, add, remove, discard
Set operationspython
tags = {"python", "beginner", "tutorial"}
tags.add("intermediate") # add one item
tags.discard("beginner") # remove if present (no error if missing)
tags.remove("tutorial") # remove (raises KeyError if missing)
print(tags) # {'python', 'intermediate'}
# Fast membership test, O(1) vs O(n) for lists
print("python" in tags) # True๐คQuick Check
You have a list with 1000 items. Which is faster for checking if a value exists?
Set math, union, intersection, difference
Set operations (math-style)python
python_devs = {"Alice", "Bob", "Charlie"}
js_devs = {"Bob", "Diana", "Charlie"}
# Union, everyone
all_devs = python_devs | js_devs
print(all_devs) # {'Alice', 'Bob', 'Charlie', 'Diana'}
# Intersection, know both
both = python_devs & js_devs
print(both) # {'Bob', 'Charlie'}
# Difference, Python only (not JS)
python_only = python_devs - js_devs
print(python_only) # {'Alice'}๐คQuick Check
What does {1, 2, 3} & {2, 3, 4} return?
๐In the Real World...
Deduplicating alert notifications so on-call engineers aren't paged 50 times for the same issue. Finding unique IP addresses in access logs. Sets are the right tool whenever uniqueness matters.
Practice Exercises
0/3 solvedExercise 1 of 3easy
โฑ 00:00Remove Duplicates
Convert the list to a set to remove duplicates, then sort and print the result.
Given: words = ["cat", "dog", "cat", "bird", "dog", "fish"]Expected output:
Given: words = ["cat", "dog", "cat", "bird", "dog", "fish"]Expected output:
['bird', 'cat', 'dog', 'fish']solution.py
1 / 3
Exercise 2 of 3easy
โฑ 00:00Common Skills
Find which skills appear in BOTH job listings.
Given:- job1 = {"Python", "SQL", "Docker", "Git"}- job2 = {"Python", "Kubernetes", "Docker", "AWS"}
Expected output (sorted):
Given:- job1 = {"Python", "SQL", "Docker", "Git"}- job2 = {"Python", "Kubernetes", "Docker", "AWS"}
Expected output (sorted):
['Docker', 'Python']solution.py
2 / 3
Exercise 3 of 3medium
โฑ 00:00Unique Email Domains
Extract the unique email domains from a list of emails.
Given: emails = ["alice@gmail.com", "bob@yahoo.com", "carol@gmail.com", "dave@outlook.com"]Expected output (sorted):
Given: emails = ["alice@gmail.com", "bob@yahoo.com", "carol@gmail.com", "dave@outlook.com"]Expected output (sorted):
['gmail.com', 'outlook.com', 'yahoo.com']solution.py
3 / 3
Solve all 3 exercises to unlock completion