Lesson 31 of 70 – Python Set Methods
44%

Python Set Methods

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.

Note: Sets are unordered collections of unique elements. Set methods are used to manage and perform operations on these elements.
1. Common Set Methods
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
2. add() Method

The add() method adds one element to a set.

numbers = {10, 20, 30}

numbers.add(40)

print(numbers)
Output:
{10, 20, 30, 40}
3. Adding a Duplicate Element

If the element already exists, add() does not create a duplicate.

numbers = {10, 20, 30}

numbers.add(20)

print(numbers)
Output:
{10, 20, 30}
4. update() Method

The update() method adds multiple elements to a set.

numbers = {10, 20}

numbers.update([30, 40, 50])

print(numbers)
Output:
{10, 20, 30, 40, 50}
5. update() with Another Set
set1 = {10, 20, 30}
set2 = {30, 40, 50}

set1.update(set2)

print(set1)
Output:
{10, 20, 30, 40, 50}

Duplicate elements are automatically ignored.

6. update() with a String

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.

7. remove() Method

The remove() method removes a specified element.

numbers = {10, 20, 30, 40}

numbers.remove(30)

print(numbers)
Output:
{10, 20, 40}
8. remove() When Element Does Not Exist

If the specified element is not present, remove() raises a KeyError.

numbers = {10, 20, 30}

numbers.remove(50)
Result:
KeyError
9. discard() Method

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)
Output:
{10, 30}
10. remove() vs discard()
Method If Element Exists If Element Does Not Exist
remove() Removes element Raises KeyError
discard() Removes element No error
11. pop() Method

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.

12. pop() with an Empty Set

Calling pop() on an empty set raises a KeyError.

numbers = set()

numbers.pop()
Result:
KeyError: 'pop from an empty set'
13. clear() Method

The clear() method removes all elements from a set.

numbers = {10, 20, 30, 40}

numbers.clear()

print(numbers)
Output:
set()
14. copy() Method

The copy() method creates a shallow copy of a set.

numbers = {10, 20, 30}

new_numbers = numbers.copy()

print(new_numbers)
Output:
{10, 20, 30}
15. Changing the Copy

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)
Output:
Original: {10, 20, 30}
Copy: {10, 20, 30, 40}
16. union() Method

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)
Output:
{1, 2, 3, 4, 5}
17. intersection() Method

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)
Output:
{3, 4}
18. difference() Method

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)
Output:
{1, 2}
19. symmetric_difference() Method

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)
Output:
{1, 2, 4, 5}
20. issubset() Method

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))
Output:
True
21. issuperset() Method

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))
Output:
True
22. isdisjoint() Method

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))
Output:
True
23. update() vs union()
Method Effect
update() Changes the original set
union() Returns a new set
set1 = {1, 2}
set2 = {2, 3}

set1.update(set2)

print(set1)
Output:
{1, 2, 3}
24. Practical Example
python_students = {"Amit", "Rahul", "Priya"}
java_students = {"Rahul", "Neha", "Amit"}

common = python_students.intersection(java_students)

print("Students learning both:")
print(common)
Output:
Students learning both:
{'Amit', 'Rahul'}
25. Key Points
  • add() adds one element.
  • update() adds multiple elements.
  • remove() removes an element and raises KeyError if it is absent.
  • discard() removes an element without raising an error if it is absent.
  • pop() removes and returns an arbitrary element.
  • clear() removes all elements.
  • copy() creates a shallow copy.
  • union() combines elements from sets.
  • intersection() finds common elements.
  • difference() finds elements unique to the first set.
  • symmetric_difference() finds elements in either set but not both.
  • issubset(), issuperset(), and isdisjoint() are useful for comparing sets.

🧠 Quick Quiz

Question: Which set method removes an element without raising an error when the element is not present?