8.5 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 collection 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 constantly check the list to see whether we’ve added the artist already, to avoid adding 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 “is a member of a set” doesn’t change after the first time we add it.
For more information on sets, see section 5.5 ( https://docs.python.org/3/tutorial/datastructures.html#sets) of the Python Tutorial. Also, the Modern Python Cookbook, Chapter 4, has several recipes related to sets.
In Python, sets can hold any hashable object,...