Python Set Methods

Python has several built-in methods that make it easy to work with sets. You can use them to add or remove items, combine sets, or check how sets are related to each other. Here are all the set methods:

Adding Element Methods

(1) add()

Adds a single element to the set. If the element is exists in the set, the set remains unchanged.

For example:

fruits = {"apple", "banana", "mango"}
fruits.add("orange")
print(fruits) # Output: {'orange', 'apple', 'banana', 'mango'}

Python sets are unordered collections, so the items may appear in different order each time you print them.

(2) update()

Adds multiple elements from an iterable (like a list, tuple, or set) to the set.

For example:

fruits = {"apple", "banana"}
fruits.update(["mango", "orange"])
print(fruits) # Output: {'orange', 'mango', 'apple', 'banana'}

If an element already exists in the set, the update() method ignores it because sets only store unique elements.

Removing Element Methods

(3) remove()

Removes the specified element from the set. Raises a KeyError if the element is not present.

For example:

fruits = {"apple", "banana", "mango"}
fruits.remove("apple")
print(fruits) # Output: {'banana', 'mango'}

# fruits.remove("orange") # Raises KeyError

(4) discard()

Removes the specified element from the set. Doesn’t raise an error if the element is missing.

For example:

fruits = {"apple", "banana", "mango"}
fruits.discard("apple")
fruits.discard("orange") # Doesn't raise an error
print(fruits) # Output: {'banana', 'mango'}

(5) pop()

Removes and returns a random element from the set.

For example:

fruits = {"apple", "banana", "mango", "orange"}
removed = fruits.pop()
print(removed) # e.g. Output: banana
print(fruits)  # Remaining Elements

(6) clear()

Removes all elements from the set.

For example:

fruits = {"apple", "banana", "mango", "orange"}
fruits.clear()
print(fruits)  # Output: set()

Set Operation (Mathematical Operation) Methods

(7) union() or |

Returns a new set containing unique elements from both sets.

For example:

a = {"apple", "banana"}
b = {"banana", "orange"}

# Using union()
c = a.union(b) 
print(c) # Output: {'orange', 'apple', 'banana'}

# Using | 
d = a | b
print(d) # Output: {'orange', 'apple', 'banana'}

(8) intersection() or &

Returns a new set containing the elements common to both sets.

For example:

a = {"apple", "banana"}
b = {"banana", "orange"}

# Using intersection()
c = a.intersection(b) 
print(c) # Output: {'banana'}

# Using &
d = a & b
print(d) # Output: {'banana'}

(9) difference() or -

Returns the elements that exists in the first set but not in the second.

For example:

a = {"apple", "banana", "pineapple"}
b = {"banana", "orange"}

# Using difference()
c = a.difference(b) 
print(c) # Output: {'apple', 'pineapple'}

# Using -
d = a - b
print(d) # Output: {'apple', 'pineapple'}

(10) symmetric_difference() or ^

Returns the elements that are in either sets, but not in both.

For example:

a = {"apple", "banana", "pineapple"}
b = {"banana", "orange"}

# Using symmetric_difference()
c = a.symmetric_difference(b) 
print(c) # Output: {'orange', 'pineapple', 'apple'}

# Using ^
d = a ^ b
print(d) # Output: {'orange', 'pineapple', 'apple'}

In-Place Update Methods

(11) intersection_update() or &=

Updates the set to include only the elements common to both sets. This method modifies the original set.

For example:

a = {"apple", "banana"}
b = {"banana", "mango"}

# Using intersection_update()
a.intersection_update(b)
print(a) # Output: {'banana'}

# Using &=
# a &= b
# print(a) # Output: {'banana'}

(12) difference_update() or -=

Removes all elements from the original set that are also present in another set.

For example:

a = {"apple", "banana"}
b = {"banana", "mango"}

# Using difference_update()
a.difference_update(b)
print(a) # Output: {'apple'}

# Using -=
# a -= b
# print(a) # Output: {'apple'}

(13) symmetric_difference_update() or ^=

Updates the set with elements that are in either sets but not in both.

For example:

a = {"apple", "banana"}
b = {"banana", "mango"}

# Using symmetric_difference_update()
a.symmetric_difference_update(b)
print(a) # Output: {'apple', 'mango'}

# Using ^=
# a ^= b
# print(a) # Output: {'apple', 'mango'}

Comparison And Membership Methods

(14) issubset() or <=

Returns True if all elements of one set are present in another.

For example:

a = {"apple", "banana"}
b = {"apple", "banana", "mango"}

# Using issubset()
print(a.issubset(b)) # Output: True

# Using <=
print(a <= b) # Output: True

(15) issuperset() or >=

Returns True if one set contains all elements of another.

For example:

a = {"apple", "banana", "mango"}
b = {"banana", "mango"}

# Using issuperset()
print(a.issuperset(b)) # Output: True

# Using >=
print(a >= b) # Output: True

(16) isdisjoint()

Returns True if the two sets have no elements in common.

For example:

a = {"apple", "banana"}
b = {"mango", "pineapple"}

print(a.isdisjoint(b)) # Output: True