Python Sets
A set in Python is used to store multiple values in a single variable. It is one of Python’s four built-in collection data types, along with List, Tuple, and Dictionary—each designed for different use cases.
What Is a Set?
A set is a collection that is:
- Unordered – Items have no fixed position
- Unindexed – Elements cannot be accessed by index
- Unchangeable – Individual items cannot be modified
- Unique – Duplicate values are not allowed
Although set items themselves cannot be changed, you can add new items or remove existing ones.
Creating a Set
Sets are written using curly braces
{}.
Characteristics of Set Items
1. Unordered
Items in a set do not follow any specific order and cannot be accessed using an index. The order of elements may differ on each execution.
2. Unchangeable (Immutable Items)
Once a set is created, you cannot change its elements directly. However, you can remove or add items.
3. No Duplicate Values
Sets automatically remove duplicate entries. Only unique values are stored.
Special Case: Boolean and Integer Values
In Python sets:
True is
treated as 1,
and False is
treated as 0.
Finding the Length of a Set
Use the
len()
function to get the number of elements in a set.
Set Items and Data Types
A set can store elements of any data type, including mixed data types.
Checking the Data Type of a Set
Using the set() Constructor
Accessing Items in a Python Set
Unlike lists or dictionaries, sets do not support indexing or keys. However,
you can still work with set elements by iterating through the set or
checking membership using the
in keyword.
Looping Through a Set
Checking if an Item Exists in a Set
Checking if an Item Does NOT Exist
Adding Items to a Python Set
In Python, set elements themselves cannot be modified, but you can still add new items or merge items from other collections after the set is created.
Adding a Single Item
Adding Items from Another Set
To combine elements from one set into another, use the
update()
method.
Adding Items from Any Iterable
Removing Items from a Python Set
1. Using remove()
2. Using discard() (Safer Option)
The
discard()
method also removes an element, but does not raise an error if the item is
missing.
3. Using pop() (Random Removal)
4. Using clear() and del
Joining Sets in Python
Python provides powerful set operations to combine or compare two or more sets.
| Method | Purpose |
|---|---|
union()
|
Combines all unique elements |
update()
|
Adds elements to an existing set |
intersection()
|
Keeps only common elements |
difference()
|
Keeps elements unique to the first set |
symmetric_difference()
|
Keeps elements not common to both sets |
Union – Combine All Elements
Returns a new set containing all unique elements from both sets. The
| operator
does the exact same thing.
Joining Multiple Sets
Intersection – Keep Common Elements Only
The
intersection()
method returns elements that exist in both sets. You can use the
& operator as
a shortcut.
Difference – Keep Unique Items from the First Set
Returns elements that exist in the first set but not in the second. The
shortcut is the
- operator.
Symmetric Difference – Exclude Common Items
Returns elements that are not shared between sets. The shortcut is the
^ operator.
Python frozenset
A frozenset is an immutable (unchangeable) version of a set. It behaves much like a normal set, but once created, its elements cannot be modified.
Even though frozensets are immutable, they support all non-modifying set
operations like
union(),
intersection(),
difference(),
and
symmetric_difference().
Subset, Superset, and Disjoint Checks
Python Set Methods Summary
| Method | Operator | Purpose |
|---|---|---|
add()
|
— | Add one element |
clear()
|
— | Remove all elements |
copy()
|
— | Copy set |
difference()
|
-
|
Unique elements |
difference_update()
|
-=
|
Modify original |
discard()
|
— | Remove safely |
intersection()
|
&
|
Common elements |
intersection_update()
|
&=
|
Modify original |
isdisjoint()
|
— | No common items |
issubset()
|
<=, <
|
Subset check |
issuperset()
|
>=, >
|
Superset check |
pop()
|
— | Remove random item |
remove()
|
— | Remove item |
symmetric_difference()
|
^
|
Non-common items |
symmetric_difference_update()
|
^=
|
Modify original |
union()
|
|
|
Combine sets |
update()
|
|=
|
Modify original |
