Structure
A set is an unordered collection of data items.
The data is not indexed, so we can’t access elements using indices or get().
This is perhaps the simplest data structure in Python. We can think of it as a bag containing random items.
svg viewer
Mutable data structures like lists or dictionaries can’t be added to a set. However, adding a tuple is perfectly fine.
One might wonder, “Why would I need a set?”
Well, a set is perfect when we simply need to keep track of the existence of items.
It doesn’t allow duplicates, which means that we can convert another data structure to a set to remove any duplicates.
Creating a Set #
The contents of a set are encapsulated in curly brackets, {}. Like all data structures, the length of a set can be calculated using len():