← Back to Python Course | Chapter 6: Data Structures | Lesson 5 of 12

Python Set Methods

Set methods let you combine bags of unique items, find what two bags share, or see what one has that the other lacks. It is like comparing two kids' sticker collections.
Syntax
python
set_name.add(item)
set_name.remove(item)
set_a.union(set_b)
set_a.intersection(set_b)

Adding and Removing Elements

add() inserts one element at a time; when removing, prefer discard() over remove() unless you specifically want an exception raised for a missing element -- discard() lets you write cleanup code without wrapping every call in try/except.

Example: Adding and Removing Elements

python
s = {1, 2, 3}
s.add(4)  # adds one element
s.discard(10)  # safe removal, no error for a missing element
print(s)

Set Union

union() merges two sets into a new one with duplicates removed automatically; the | operator does the exact same thing with less typing, and both work with any number of sets chained together (a | b | c).

Example: Set Union

python
a = {1, 2}
b = {2, 3}
print(a.union(b))  # merges both sets, dropping duplicates
print(a | b)  # same result using the operator shorthand

Set Intersection

intersection() returns only the elements common to both sets, and & is its operator shorthand -- a typical use is finding which tags two articles share, or which users are in two different groups.

Example: Set Intersection

python
a = {1, 2, 3}
b = {2, 3, 4}
print(a.intersection(b))  # elements common to both
print(a & b)  # same result using the operator shorthand

Set Difference

difference() returns what's in the first set but not the second (- is the shorthand), while symmetric_difference() returns elements unique to either set alone -- the two are easy to confuse, so it helps to think of symmetric_difference as 'the XOR of two sets'.

Example: Set Difference

python
a = {1, 2, 3}
b = {2, 3}
print(a.difference(b))  # elements only in a
print(a.symmetric_difference(b))  # elements in exactly one of the two

Checking Set Relationships

issubset() checks whether all of a set's elements exist in another set, issuperset() checks the reverse relationship, and isdisjoint() confirms two sets share zero elements in common -- useful for quickly detecting conflicting or overlapping selections.

Example: Checking Set Relationships

python
a = {1, 2}
b = {1, 2, 3}
c = {5, 6}
print(a.issubset(b))  # every element of a is in b
print(b.issuperset(a))  # the reverse relationship
print(a.isdisjoint(c))  # True, a and c share no elements
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using remove() on a missing element, which raises KeyError, when discard() does nothing if the element is absent.
  2. Expecting a.union(b) to change a, when it returns a new set; use a.update(b) to modify in place.
  3. Confusing pop() with list pop(), since a set's pop() removes an arbitrary element, not the last one added.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.