Python Basics — Set Methods

2 minute read

Set Methods

Sets have methods for adding, removing, and updating elements — plus in-place versions of the set operations.

Adding Elements

.add() — Single Element

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

# Adding a duplicate does nothing
s.add(2)
print(s)  # {1, 2, 3, 4}

Removing Elements

.remove() — Raises KeyError

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

# s.remove(99)  # KeyError: 99

.discard() — No Error If Missing

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

s.discard(99)  # No error
print(s)       # {1, 3}

Rule: Use .discard() when you’re not sure if the element exists. Use .remove() when you need to know if something was missing.

.pop() — Remove Arbitrary Element

s = {1, 2, 3}
item = s.pop()
print(item)  # 1 (or 2 or 3 — arbitrary)
print(s)     # remaining elements

# s = set()
# s.pop()  # KeyError: 'pop from an empty set'

.clear() — Remove All

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

In-Place Update Methods

These methods modify the set instead of returning a new one.

.update() — In-Place Union

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

# Equivalent to: a |= {3, 4, 5}

.intersection_update() — In-Place Intersection

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

# Equivalent to: a &= {2, 3, 5}

.difference_update() — In-Place Difference

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

# Equivalent to: a -= {2, 4}

.symmetric_difference_update() — In-Place Symmetric Difference

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

# Equivalent to: a ^= {2, 3, 4}

remove() vs discard() Comparison

Feature remove(x) discard(x)
Element exists Removes it Removes it
Element missing KeyError No error
Use when Absence is a bug Absence is OK

Complete Methods Reference

Method Description Returns
s.add(x) Add element None
s.remove(x) Remove (KeyError if missing) None
s.discard(x) Remove (no error if missing) None
s.pop() Remove and return arbitrary element Element
s.clear() Remove all elements None
s.update(other) Add all from other (union in-place) None
s.intersection_update(other) Keep only common (intersection in-place) None
s.difference_update(other) Remove all found in other None
s.symmetric_difference_update(other) Keep only non-common None
s.union(other) Return new union set set
s.intersection(other) Return new intersection set set
s.difference(other) Return new difference set set
s.symmetric_difference(other) Return new sym diff set set
s.issubset(other) All of s in other? bool
s.issuperset(other) All of other in s? bool
s.isdisjoint(other) No elements in common? bool
s.copy() Shallow copy set

Summary

  • .add(x) adds one element; .update(iterable) adds many
  • .remove(x) raises KeyError; .discard(x) doesn’t
  • .pop() removes an arbitrary element
  • In-place operations: update, intersection_update, difference_update
  • All in-place methods return None — they modify the set directly

Next up: Frozenset →

Updated: