Comprehensive guide to Python set methods
In this article, we will explore the various methods available for Python sets, which are unordered collections of unique elements. Sets can be used for a wide range of tasks, such as removing duplicates, performing set operations like union, intersection, and difference, and testing membership and subsets. By mastering the set methods, you can enhance your Python skills and write more efficient and elegant code.
Creating and initializing sets
To create a set, you can use the curly braces {} notation or the built-in set() function. If you want to initialize a set with some elements, you can use the same syntax as for lists, tuples, or strings, but without duplicates. For example:
Make sets in Python
fruits = {'apple', 'banana', 'cherry'}
colors = set(['red', 'green', 'blue'])
vowels = set('aeiou')Note that the order of the elements is arbitrary and may change between runs, as sets are implemented using hash tables.
Basic set operations
Once you have created some sets, you can perform various operations on them. The most common ones are:
- Union: combines the elements of two or more sets into a new set that contains all the unique elements.
Union sets in Python
A = {1, 2, 3}
B = {2, 3, 4}
C = {3, 4, 5}
print(A.union(B, C))- Intersection: finds the common elements of two or more sets, i.e., the elements that belong to all of them.
Sets intersection in Python
A = {1, 2, 3}
B = {2, 3, 4}
C = {3, 4, 5}
print(A.intersection(B, C))- Difference: removes the elements of one set from another set, i.e., the elements that belong only to the first set.
Sets difference in Python
A = {1, 2, 3}
B = {2, 3, 4}
A.difference(B)- Symmetric difference: finds the elements that belong to exactly one of two sets, i.e., the elements that are not common to both sets.
Sets symmetric difference in Python
A = {1, 2, 3}
B = {2, 3, 4}
print(A.symmetric_difference(B))- Subset and superset: check if one set is a subset or a superset of another set, i.e., if all the elements of the first set are also in the second set, or vice versa.
subset and superset in Python
A = {1, 2, 3}
B = {2, 3, 4}
C = {3, 4, 5}
print(A.issubset(B))
# False
print(A.issubset(A.union(B)))
# True
print(A.issuperset(B))
# False
print(A.union(B).issuperset(A))
# TrueModifying sets
Sets are mutable objects, meaning that you can add or remove elements from them, or update them with the elements of other sets. The most common methods for modifying sets are:
- Add: inserts a new element into the set, if it is not already present.
Add element to a Python set
A = {1, 2, 3}
A.add(0)
print(A)- Remove: removes an element from the set, raising a
KeyErrorif it is not present.
Remove element from a Python list
A = {0, 1, 2, 3}
A.remove(0)
print(A)- Discard: removes an element from the set, without raising an error if it is not present.
Discard element from a Python list
A = {0, 1, 2, 3}
A.discard(0)
print(A)- Pop: removes and returns an arbitrary element from the set, raising a
KeyErrorif it is empty.
Pop from a Python set
A = {1, 2, 3}
A.pop()
print(A)- Update: adds the elements of one or more sets into the set, ignoring duplicates.
Update a set with another set in Python
A = {1, 2, 3}
B = {2, 3, 4}
A.update(B)
print(A)- Clear: removes all the elements from the set, making it empty.
Clear a set in Python
A = {1, 2, 3}
A.clear()
print(A)Set methods summary
Here is a summary of the set methods we have covered in this article:
| Method name | Description |
|---|---|
union(*others) | Returns a new set with the union of the current set and one or more other sets. |
intersection(*others) | Returns a new set with the intersection of the current set and one or more other sets. |
difference(*others) | Returns a new set with the difference of the current set and one or more other sets. |
symmetric_difference(other) | Returns a new set with the symmetric difference of the current set and another set. |
issubset(other) | Returns True if the current set is a subset of another set. |
issuperset(other) | Returns True if the current set is a superset of another set. |
add(elem) | Adds an element to the current set. |
remove(elem) | Removes an element from the current set, raising an error if it is not present. |
discard(elem) | Removes an element from the current set, without raising an error if it is not present. |
pop() | Removes and returns an arbitrary element from the current set, raising an error if it is empty. |
update(*others) | Adds the elements of one or more other sets to the current set. |
Practice
Which of the following are methods that can be used on sets in Python?