Python – Set Methods
Python provides several built-in methods for working with sets. These methods allow you to perform various operations such as adding, removing, and modifying elements in a set. Here is a list of the commonly used set methods:
1. add()
Adds an element to the set. If the element already exists in the set, it does nothing (sets do not allow duplicates).
Syntax:
set.add(element)
Example:
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
2. clear()
Removes all elements from the set, leaving it empty.
Syntax:
set.clear()
Example:
my_set = {1, 2, 3}
my_set.clear()
print(my_set) # Output: set()
3. copy()
Returns a shallow copy of the set. Changes to the new set will not affect the original set.
Syntax:
new_set = set.copy()
Example:
my_set = {1, 2, 3}
new_set = my_set.copy()
print(new_set) # Output: {1, 2, 3}
4. discard()
Removes the specified element from the set if it exists. If the element is not found, it does nothing (unlike remove(), which raises an error if the element is not found).
Syntax:
set.discard(element)
Example:
my_set = {1, 2, 3}
my_set.discard(2)
print(my_set) # Output: {1, 3}
my_set.discard(5) # No error even though 5 doesn't exist
5. remove()
Removes the specified element from the set. If the element is not found, it raises a KeyError.
Syntax:
set.remove(element)
Example:
my_set = {1, 2, 3}
my_set.remove(2)
print(my_set) # Output: {1, 3}
# my_set.remove(5) # Raises KeyError as 5 is not in the set
6. pop()
Removes and returns a random element from the set. If the set is empty, it raises a KeyError.
Syntax:
element = set.pop()
Example:
my_set = {1, 2, 3}
removed_element = my_set.pop()
print(removed_element) # Random element, e.g., 1
print(my_set) # Set with one element removed
7. union()
Returns a new set containing all elements from the set and another iterable (or set), without duplicates.
Syntax:
set1.union(set2)
Example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result_set = set1.union(set2)
print(result_set) # Output: {1, 2, 3, 4, 5}
8. update()
Adds all elements from another iterable (or set) to the current set. It modifies the original set in place and does not return a value.
Syntax:
set.update(iterable)
Example:
set1 = {1, 2, 3}
set2 = {4, 5}
set1.update(set2)
print(set1) # Output: {1, 2, 3, 4, 5}
9. intersection()
Returns a new set containing only the elements that are common between the set and another iterable (or set).
Syntax:
set1.intersection(set2)
Example:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
result_set = set1.intersection(set2)
print(result_set) # Output: {2, 3}
10. intersection_update()
Updates the current set with the intersection of itself and another iterable (or set). This method modifies the original set in place.
Syntax:
set1.intersection_update(set2)
Example:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set1.intersection_update(set2)
print(set1) # Output: {2, 3}
11. difference()
Returns a new set containing the elements that are in the current set but not in another set (set difference).
Syntax:
set1.difference(set2)
Example:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
result_set = set1.difference(set2)
print(result_set) # Output: {1}
12. difference_update()
Updates the current set with the difference between itself and another set. This method modifies the original set in place.
Syntax:
set1.difference_update(set2)
Example:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set1.difference_update(set2)
print(set1) # Output: {1}
13. symmetric_difference()
Returns a new set containing the elements that are in either of the sets, but not in both.
Syntax:
set1.symmetric_difference(set2)
Example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result_set = set1.symmetric_difference(set2)
print(result_set) # Output: {1, 2, 4, 5}
14. symmetric_difference_update()
Updates the current set with the symmetric difference of itself and another set. This method modifies the original set in place.
Syntax:
set1.symmetric_difference_update(set2)
Example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.symmetric_difference_update(set2)
print(set1) # Output: {1, 2, 4, 5}
15. issubset()
Returns True if all elements of the set are present in another set or iterable.
Syntax:
set1.issubset(set2)
Example:
set1 = {1, 2}
set2 = {1, 2, 3}
print(set1.issubset(set2)) # Output: True
16. issuperset()
Returns True if all elements of another set are contained within the current set.
Syntax:
set1.issuperset(set2)
Example:
set1 = {1, 2, 3}
set2 = {2, 3}
print(set1.issuperset(set2)) # Output: True
17. isdisjoint()
Returns True if the set has no elements in common with another set or iterable.
Syntax:
set1.isdisjoint(set2)
Example:
set1 = {1, 2, 3}
set2 = {4, 5}
print(set1.isdisjoint(set2)) # Output: True
Summary of Common Set Methods:
add(): Add an element.clear(): Remove all elements.copy(): Return a shallow copy.discard(): Remove an element (no error if not found).remove(): Remove an element (raisesKeyErrorif not found).pop(): Remove and return a random element.union(): Combine sets without duplicates.update(): Add elements from another set (modifies in place).intersection(): Find common elements.difference(): Find elements only in the first set.symmetric_difference(): Find elements in either set, but not both.issubset(),issuperset(),isdisjoint(): Set comparison operations.