Sets

Learn Python sets to ensure unique elements, perform set operations, and optimize membership checks over lists.

Why do we need sets?

Lists are extremely versatile tools that suit many container object applications. But they are not useful when we want to ensure that objects in a list are unique. For example, a song library may contain many songs by the same artist. If we want to sort through the library and create a list of all the artists, we would have to check the list to see whether we’ve added the artist already before we add them again.

This is where sets come in. Sets come from mathematics, where they represent an unordered group of unique items. We can try to add an item to a set five times, but the “is a member of a set” doesn’t change after the first time we add it.

Example

In Python, sets can hold any hashable object, not just strings or numbers. Hashable objects implement the __hash__() method; these are the same objects that can be used as keys in dictionaries, so again, mutable lists, sets, and dictionaries are out. Like mathematical sets, they can store only one copy of each object. If we’re trying to create a list of song artists, we can create a set of string names and add them to the set. This example starts with a list of (song, artist) tuples and creates a set of the artists:

Press + to interact
song_library = [
("Phantom Of The Opera", "Sarah Brightman"),
("Knocking On Heaven's Door", "Guns N' Roses"),
("Captain Nemo", "Sarah Brightman"),
("Patterns In The Ivy", "Opeth"),
("November Rain", "Guns N' Roses"),
("Beautiful", "Sarah Brightman"),
("Mal's Song", "Vixy and Tony"),
]
artists = set()
for song, artist in song_library:
artists.add(artist)

There is no built-in syntax for an empty set as there is for lists and dictionaries; we create a set using the set() constructor. However, we can use the curly braces (borrowed from dictionary syntax) to create a set, so long as the set contains values. If we use colons to separate pairs of values, it’s ...