Python provides several built-in methods for working with sets. These methods allow you to add, remove, copy, and perform different operations on set elements.
| Method | Purpose |
|---|---|
| add() | Adds one element |
| update() | Adds multiple elements |
| remove() | Removes an element |
| discard() | Removes an element without raising an error if absent |
| pop() | Removes and returns an arbitrary element |
| clear() | Removes all elements |
| copy() | Creates a copy of a set |
The add() method adds one element to a set.
numbers = {10, 20, 30}
numbers.add(40)
print(numbers)
If the element already exists, add() does not create a duplicate.
numbers = {10, 20, 30}
numbers.add(20)
print(numbers)
The update() method adds multiple elements to a set.
numbers = {10, 20}
numbers.update([30, 40, 50])
print(numbers)
set1 = {10, 20, 30}
set2 = {30, 40, 50}
set1.update(set2)
print(set1)
Duplicate elements are automatically ignored.
update() can add elements from any iterable. A string is treated as a sequence of characters.
letters = {"a", "b"}
letters.update("cat")
print(letters)
The characters from the string are added individually. Since sets do not contain duplicates, repeated characters are stored only once.
The remove() method removes a specified element.
numbers = {10, 20, 30, 40}
numbers.remove(30)
print(numbers)
If the specified element is not present, remove() raises a KeyError.
numbers = {10, 20, 30}
numbers.remove(50)
The discard() method removes an element if it exists. If the element does not exist, no error is raised.
numbers = {10, 20, 30}
numbers.discard(20)
print(numbers)
| Method | If Element Exists | If Element Does Not Exist |
|---|---|---|
| remove() | Removes element | Raises KeyError |
| discard() | Removes element | No error |
The pop() method removes and returns an arbitrary element from a set.
numbers = {10, 20, 30}
value = numbers.pop()
print("Removed:", value)
print("Set:", numbers)
Because sets are unordered, you should not assume which element pop() will remove.
Calling pop() on an empty set raises a KeyError.
numbers = set()
numbers.pop()
The clear() method removes all elements from a set.
numbers = {10, 20, 30, 40}
numbers.clear()
print(numbers)
The copy() method creates a shallow copy of a set.
numbers = {10, 20, 30}
new_numbers = numbers.copy()
print(new_numbers)
Changing the copied set does not change the original set.
numbers = {10, 20, 30}
new_numbers = numbers.copy()
new_numbers.add(40)
print("Original:", numbers)
print("Copy:", new_numbers)
The union() method returns a new set containing elements from both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2)
print(result)
The intersection() method returns elements that are common to both sets.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.intersection(set2)
print(result)
The difference() method returns elements that are present in the first set but not in the second set.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.difference(set2)
print(result)
The symmetric_difference() method returns elements that belong to either set but not to both.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.symmetric_difference(set2)
print(result)
The issubset() method checks whether all elements of one set are present in another set.
set1 = {1, 2}
set2 = {1, 2, 3, 4}
print(set1.issubset(set2))
The issuperset() method checks whether a set contains all elements of another set.
set1 = {1, 2, 3, 4}
set2 = {1, 2}
print(set1.issuperset(set2))
The isdisjoint() method returns True if two sets have no elements in common.
set1 = {1, 2, 3}
set2 = {4, 5, 6}
print(set1.isdisjoint(set2))
| Method | Effect |
|---|---|
| update() | Changes the original set |
| union() | Returns a new set |
set1 = {1, 2}
set2 = {2, 3}
set1.update(set2)
print(set1)
python_students = {"Amit", "Rahul", "Priya"}
java_students = {"Rahul", "Neha", "Amit"}
common = python_students.intersection(java_students)
print("Students learning both:")
print(common)
Question: Which set method removes an element without raising an error when the element is not present?