Sets
Learn how to manage sets in Python through operations like creation, addition, deletion, iteration, and comprehension.
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.
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?” A set is ideal when we need to keep track of the existence of unique items. For example, some students might have registered for a course multiple times, leading to duplicate entries in the list.
Since sets cannot contain duplicates, converting the list to a set will remove any duplicates. Other set ...