Intermediate · 11 min
Sets and unique values
Remove duplicates and compare collections with set operations.
Keep unique items
A set stores each value once. Converting a list to a set removes duplicates; sorting gives a predictable display order.
tags = ["python", "web", "python"]
unique = sorted(set(tags))
print(unique)Output
['python', 'web']
Find shared values
The & operator returns the intersection: values present in both sets.
team_a = {"Ada", "Mina"}
team_b = {"Mina", "Leo"}
print(sorted(team_a & team_b))Output
['Mina']
Sets and unique values
- Read both sets and unique values examples before answering.
- Run the task, compare its exact output, and revise the code if needed.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Find new attendees
Create newcomers containing names in today but not yesterday. Display the names in sorted order.
Set subtraction uses today - yesterday.