Set Methods

Python sets come with powerful built-in methods that allow you to add, remove, compare, and combine sets efficiently. Because sets show unique and unordered data, these methods are widely used in data cleaning, comparisons, and logic building.
Below is a complete, method-by-method explanation, with intro, syntax, and example for each.

add()

What it does

Adds one element to the set.

Syntax

python
set.add(element)

Example

python
fruits = {"apple", "banana"}
fruits.add("cherry")
print(fruits)

update()

What it does

Adds multiple elements from an iterable.

Syntax

python
set.update(iterable)

Example

python
numbers = {1, 2}
numbers.update([3, 4, 5])
print(numbers)

remove()

What it does

Removes a specified element. Raises error if element not found.

Syntax

python
set.remove(element)

Example

python
fruits = {"apple", "banana"}
fruits.remove("banana")
print(fruits)

discard()

What it does

Removes element without raising error if missing.

Syntax

python
set.discard(element)

Example

python
fruits.discard("mango")
print(fruits)

pop()

What it does

Removes and returns a random element.

Syntax

python
set.pop()

Example

python
items = {1, 2, 3}
x = items.pop()
print(x)
print(items)

clear()

What it does

Removes all elements from the set.

Syntax

python
set.clear()

Example

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

union()

What it does

Returns a new set containing all unique elements.

Syntax

python
set1.union(set2)

Example

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

intersection()

What it does

Returns elements common to both sets.

Syntax

python
set1.intersection(set2)

Example

python
print(a.intersection(b))

difference()

What it does

Returns elements in first set but not in second.

Syntax

python
set1.difference(set2)

Example

python
print(a.difference(b))

symmetric_difference()

What it does

Returns elements not common to both sets.

Syntax

python
set1.symmetric_difference(set2)

Example

python
print(a.symmetric_difference(b))

intersection_update()

What it does

Updates the set keeping only common elements.

Syntax

python
set.intersection_update(set2)

Example

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

difference_update()

What it does

Removes elements found in another set.

Syntax

python
set.difference_update(set2)

Example

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

symmetric_difference_update()

What it does

Keeps only elements not common.

Syntax

python
set.symmetric_difference_update(set2)

Example

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

issubset()

What it does

Checks if one set is a subset of another.

Syntax

python
set1.issubset(set2)

Example

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

issuperset()

What it does

Checks if one set contains another set.

Syntax

python
set1.issuperset(set2)

Example

python
print(b.issuperset(a))

isdisjoint()

What it does

Checks if two sets have no common elements.

Syntax

python
set1.isdisjoint(set2)

Example

python
x = {1, 2}
y = {3, 4}
print(x.isdisjoint(y))

All Set Methods – Summary Table

MethodPurpose
add()Add one element
update()Add multiple elements
remove()Remove element (error if missing)
discard()Remove safely
pop()Remove random element
clear()Remove all
union()Combine sets
intersection()Common elements
difference()Unique to first
symmetric_difference()Non-common
issubset()Subset check
issuperset()Superset check
isdisjoint()No common elements

Key Notes

  • Sets store only unique values
  • Order is not preserved
  • Mutable but elements must be immutable
  • Extremely fast for membership testing
This topic completes the entire Python Set concept in a clean, interview-ready way.