Sets

Sets#

One final useful structure in Python is the set. While a dictionary maps (unique) keys to values, a set stores a collection of unique elements. Here’s an example:

Like it does for lists and dicts, Python supports a special notation for sets: curly braces. You might be confused… don’t dictionaries use those, too? They do, and it can be tricky!

If {} defaults to a dictionary, how then do you write the empty set? Easy: you use the set() constructor:

Make sure to run help(set) to learn more about set operations in Python.