About Python Sets
tags: #python/documentation/sets
What are Python Sets?
A Python set is one of 4 built-in data types in Python used to store collections of data that is:
- Unordered
- Unique
- Mutable
This object is based on set theory.
A set can be thought of simply as a well-defined collection of distinct objects, typically called elements or members.
Sets are commonly used in situations where you need to store a collection of unique elements, such as removing duplicates from a list, checking membership, or performing set operations like union, intersection, and difference.
Creating a Python Set
Sets are defined by enclosing elements in curly braces, separated by commas.
my_set = {val1, val2, val3,...}
Alternatively, we can create a set using the set() constructor function:
my_set = set(<iterable>)
Operating on a Set
Many common set operations in set theory can be applied to Python set objects: