Python Set Methods: A Complete Guide with Examples

Last updated 1 month, 3 weeks ago | 126 views 75     5

Tags:- Python

A set in Python is an unordered collection of unique elements. Sets are useful when you need to ensure all elements are distinct or when you need to perform mathematical set operations like union, intersection, and difference.

Python provides several built-in set methods to manipulate and analyze sets. This guide covers all major set methods, each with clear examples.


Creating a Set

my_set = {1, 2, 3, 4}

You can also create a set using the set() constructor:

my_set = set([1, 2, 3])

Set Methods

✅ 1. add()

Adds a single element to the set.

s = {1, 2}
s.add(3)
print(s)

Output: {1, 2, 3}


✅ 2. clear()

Removes all elements from the set.

s = {1, 2, 3}
s.clear()
print(s)

Output: set()


✅ 3. copy()

Returns a shallow copy of the set.

s = {1, 2, 3}
copy_set = s.copy()
print(copy_set)

Output: {1, 2, 3}


✅ 4. difference()

Returns a new set with elements that are in the first set but not in the others.

a = {1, 2, 3}
b = {2, 3, 4}
print(a.difference(b))

Output: {1}


✅ 5. difference_update()

Removes elements from the set that are also in another set.

a = {1, 2, 3}
b = {2, 3}
a.difference_update(b)
print(a)

Output: {1}


✅ 6. discard()

Removes an element if it exists. Does nothing if the element is not present.

s = {1, 2, 3}
s.discard(2)
s.discard(5)  # No error
print(s)

Output: {1, 3}


✅ 7. intersection()

Returns a new set with common elements.

a = {1, 2, 3}
b = {2, 3, 4}
print(a.intersection(b))

Output: {2, 3}


✅ 8. intersection_update()

Updates the set to keep only common elements.

a = {1, 2, 3}
b = {2, 3, 4}
a.intersection_update(b)
print(a)

Output: {2, 3}


✅ 9. isdisjoint()

Returns True if sets have no elements in common.

a = {1, 2}
b = {3, 4}
print(a.isdisjoint(b))

Output: True


✅ 10. issubset()

Returns True if all elements of the set are in another set.

a = {1, 2}
b = {1, 2, 3}
print(a.issubset(b))

Output: True


✅ 11. issuperset()

Returns True if the set contains all elements of another set.

a = {1, 2, 3}
b = {1, 2}
print(a.issuperset(b))

Output: True


✅ 12. pop()

Removes and returns an arbitrary element.

s = {10, 20, 30}
removed = s.pop()
print(removed)
print(s)

Output:
Varies — e.g., 10
Remaining set: {20, 30}


✅ 13. remove()

Removes the specified element. Raises an error if not found.

s = {1, 2, 3}
s.remove(2)
print(s)

Output: {1, 3}

❗ Raises KeyError if the element doesn’t exist.


✅ 14. symmetric_difference()

Returns a new set with elements in either set, but not both.

a = {1, 2, 3}
b = {3, 4, 5}
print(a.symmetric_difference(b))

Output: {1, 2, 4, 5}


✅ 15. symmetric_difference_update()

Updates the set with the symmetric difference of itself and another set.

a = {1, 2, 3}
b = {3, 4}
a.symmetric_difference_update(b)
print(a)

Output: {1, 2, 4}


✅ 16. union()

Returns a new set with all elements from both sets.

a = {1, 2}
b = {2, 3}
print(a.union(b))

Output: {1, 2, 3}


✅ 17. update()

Adds elements from another set to the current set.

a = {1, 2}
b = {3, 4}
a.update(b)
print(a)

Output: {1, 2, 3, 4}


Summary Table

Method Description
add() Adds a single element
clear() Removes all elements
copy() Returns a shallow copy
difference() Elements in one set but not the other(s)
difference_update() Removes common elements from the original set
discard() Removes an element if present
intersection() Common elements in sets
intersection_update() Keeps only common elements
isdisjoint() Returns True if sets have no common elements
issubset() Checks if set is a subset
issuperset() Checks if set is a superset
pop() Removes and returns a random element
remove() Removes a specific element
symmetric_difference() Elements in either set, but not both
symmetric_difference_update() Updates the set with symmetric difference
union() All elements from both sets
update() Adds elements from another set

Final Thoughts

Python’s set methods offer powerful and concise tools for managing collections of unique elements. They are especially useful for operations like deduplication, mathematical computations, and comparisons.