#Python's set
A set is a fundamental mathematical model representing a collection of distinct objects. It is similar to a dictionary with only keys — in simple terms, elements in a set are unique.
Set literals are defined using curly braces ({}
) with a group of values, for example:
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'}
The type annotation set[str]
means a set where the element type is str
.
#Empty Set
To create an empty set, use set()
instead of {}
, as the latter creates an empty dictionary.
#Creating Sets from a List or Tuple
You can create a set from a list or tuple. Duplicate values in the source will be filtered out, effectively deduplicating the data:
numbers_list:list[int] = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6]
numbers_set:set[int] = set(numbers_list)
print(numbers_list)
print(numbers_set)
numbers_list = list(numbers_set)
print(numbers_list)
#Checking Element Existence
Just like dictionaries, you can use in
to check if an element exists in a set:
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'}
print('Strawberry' in fruits)
print('Strawberry' not in fruits)
#Adding Elements
Use the add
method to add elements:
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'}
fruits.add('Grape')
print(fruits)
#Removing Elements
Use remove
or discard
to delete elements:
remove
– deletes the specified element, raises an error if not found.discard
– deletes the specified element, does nothing if not found.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'}
fruits.remove('Apple') # Remove 'Apple'
fruits.discard('Banana') # Remove 'Banana'
print(fruits)
#Clearing a Set
Use the clear
method to remove all elements:
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'}
fruits.clear()
print(fruits)
#Merging Sets
Use the update
method to merge another set:
fruits:set[str] = {'Apple', 'Orange', 'Strawberry'}
fruits.update({'Banana', 'Pineapple'})
print(fruits)
#Intersection
Use the intersection
method or &
operator to get the intersection of sets (elements common to both):
- Both methods return a new set without modifying the original.
- Use
intersection_update
to modify the original set in place.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Tomato', 'Cucumber'}
vegetables:set[str] = {'Cabbage', 'Tomato', 'Cucumber', 'Spinach'}
print(fruits.intersection(vegetables))
print(fruits & vegetables)
#Union
Use the union
method or |
operator to get the union of sets (all unique elements from both):
- Both methods return a new set without modifying the original.
- Use
update
to modify the original set in place.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Tomato', 'Cucumber'}
vegetables:set[str] = {'Cabbage', 'Tomato', 'Cucumber', 'Spinach'}
print(fruits.union(vegetables))
print(fruits | vegetables)
#Difference
Use the difference
method or -
operator to get the difference (elements only in the first set):
- Both methods return a new set without modifying the original.
- Use
difference_update
to modify the original set in place.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Tomato', 'Cucumber'}
vegetables:set[str] = {'Cabbage', 'Tomato', 'Cucumber', 'Spinach'}
print(fruits.difference(vegetables))
print(fruits - vegetables)
#Symmetric Difference
Use the symmetric_difference
method to get the symmetric difference (elements unique to each set):
- Both methods return a new set without modifying the original.
- Use
symmetric_difference_update
to modify the original set in place.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Tomato', 'Cucumber'}
vegetables:set[str] = {'Cabbage', 'Tomato', 'Cucumber', 'Spinach'}
print(fruits.symmetric_difference(vegetables))
#Subsets and Supersets
If all elements of set A are in set B, then A is a subset of B, and B is a superset of A. If A and B are not equal, then A is a proper subset of B, and B is a proper superset of A.
Python uses comparison operators to check set relationships:
Relation | Description |
---|---|
A <= B | A is a subset of B |
A < B | A is a proper subset of B |
A >= B | A is a superset of B |
A > B | A is a proper superset of B |