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:

Get hands-on with 1200+ tech skills courses.