You already know that we store dictionary keys in a single instance in the dictionary. Adding a new value to an existing key replaces the old value. This single instance storage property is often handy in cases where we want to store the keys themselves, not their values.
For example, suppose we want to store a list of cities your users have visited. We do not need to duplicate the record if they travel to the town twice. It saves storage space and makes it easier to find information. We may also need to find out which cities were visited by both Vasya and Masha and which were visited only by Masha (or only by Vasya).
In mathematics, sets are used to:
- To store a list of elements in certain sets
- To compare these sets with each other (abstract_data_type)
Python provides a data structure of the same name - set
. Thus, Python sets are unordered sequences of elements, where we represent each element only once in the set.
How to create sets and manipulate them
We can create a set using the corresponding literal: