Originally published on CodingCush .
A Python set is a collection that holds unique items in no particular order. Add the same value twice and the set keeps only one copy. If you need to remove duplicates from a list, test whether something is in a group, or find what two groups have in common, a set is usually the right tool. In this guide you will learn how to create sets, add and remove items, test membership, and use union, intersection and difference, with small examples run on Python 3.12.3. All outputs are real.
What Is a Set, in One Minute?
Think of a set as a bag that refuses to hold two copies of the same thing. Put in "apple", "orange", "apple" and "pear", and the bag holds three items, because the second "apple" is dropped. Order does not matter either: a set does not promise to keep items in the order you added them.
Here is the classic demo from the official Python tutorial.…