🗂️ Dictionaries & Sets, lesson 4 of 5
Sets: unique values
Drop duplicates and test membership fast with sets.
8 min
1 exercise
2 quizzes
0/3 solved
Getting Python ready… examples can run in a moment.
A set is an unordered collection of unique
values. Adding something that's already there does
nothing. Sets are perfect for removing duplicates and
for super-fast in checks.
tags = {"python", "beginner"}
empty = set() # not {} (that's a dict!)
Remove duplicates
sorted() turns the set into a tidy, ordered list
for display. Try printing unique directly.
Add and remove
remove raises a KeyError if the item is missing; discard quietly does nothing. Try tags.remove("java").
What does this print?
Sets can't be indexed
Need the items in order? sorted(colors)[0] works.
What does this print?
Unique visitors
The same people signed in with different
capitalisation. Add every name from log to
unique_names in lower case, then set count
to the number of different people.