PYTHON Reference Guide
Revision Time: 4 mins

Set Methods Reference

Deduplication and set math operators (intersections, unions).

add
Return: None

Add element to set.

Used In: Deduplicating streaming IDs, tracking visited paths.

Syntax signature:set.add(x)
Code snippet:
python
s = {1, 2}
s.add(2)
print(s)
Expected Output:{1, 2}

Time Complexity: O(1)

Common Mistakes: Instantiating an empty set using s = {} actually creates an empty dict; use s = set() instead.

Related Methods: update(), discard()

Comparison:

add() vs update(): add(x) inserts a single element x into the set; update(iterable) inserts all elements from the iterable into the set.

Remember: Sets only store unique, hashable elements.
update
Return: None

Add all elements from an iterable to the set.

Used In: Batch record deduplication.

Syntax signature:set.update(iterable)
Code snippet:
python
s = {1}
s.update([2, 3])
print(s)
Expected Output:{1, 2, 3}

Time Complexity: O(K) where K is size of iterable

Related Methods: add()

Remember: Ideal for combining lists of unique elements in-place.
remove
Return: None

Remove element from set; raise KeyError if missing.

Used In: Deleting active tags.

Syntax signature:set.remove(x)
Code snippet:
python
s = {1, 2}
s.remove(1)
print(s)
Expected Output:{2}

Time Complexity: O(1)

Related Methods: discard()

Comparison:

remove() vs discard(): remove() throws KeyError if the element is missing; discard() fails silently.

Remember: Throws KeyError if the item does not exist. Use discard() to remove safely.
discard
Return: None

Remove element from set safely without throwing errors if missing.

Used In: Tearing down optional flag sets.

Syntax signature:set.discard(x)
Code snippet:
python
s = {1, 2}
s.discard(3)
print(s)
Expected Output:{1, 2}

Time Complexity: O(1)

Related Methods: remove()

Remember: Very useful when clearing items where existence is uncertain.
pop
Return: T

Remove and return an arbitrary element from the set.

Used In: Consuming unique item streams.

Syntax signature:set.pop()
Code snippet:
python
s = {10, 20}
v = s.pop()
print(type(v))
Expected Output:<class 'int'>

Time Complexity: O(1)

Related Methods: remove()

Remember: Raises KeyError if the set is empty. Set elements are unordered, so the popped item is arbitrary.
union
Return: Set[T]

Return a new set containing elements from all sets.

Used In: Aggregating unique identifiers across lists.

Syntax signature:set.union(*others)
Code snippet:
python
s1 = {1}
s2 = {2}
print(s1.union(s2))
Expected Output:{1, 2}

Time Complexity: O(S + T) where S, T are set sizes

Related Methods: update()

Remember: Can be written using the pipe (|) operator: s1 | s2.
intersection
Return: Set[T]

Get common elements between sets.

Used In: Finding overlapping keys, filtering datasets.

Syntax signature:set.intersection(*others)
Code snippet:
python
s1 = {'a', 'b'}
s2 = {'b', 'c'}
print(s1.intersection(s2))
Expected Output:{'b'}

Time Complexity: O(min(len(s1), len(s2)))

Common Mistakes: Using operator & requires both operands to be sets, whereas method intersection() accepts any iterable.

Related Methods: union(), difference()

Comparison:

set.intersection() vs set.difference(): intersection() returns common elements; difference() returns elements present in s1 but absent in s2.

Remember: Can also use the & operator: s1 & s2.
difference
Return: Set[T]

Return elements that exist in the set but not in others.

Used In: Finding missing keys, pipeline audit gaps.

Syntax signature:set.difference(*others)
Code snippet:
python
s1 = {1, 2, 3}
s2 = {2}
print(s1.difference(s2))
Expected Output:{1, 3}

Time Complexity: O(len(s1))

Related Methods: intersection()

Remember: Can be written using minus (-) operator: s1 - s2.
issubset
Return: bool

Check if all elements of the set exist in another set.

Used In: Validating categorical domains.

Syntax signature:set.issubset(other)
Code snippet:
python
{1, 2}.issubset({1, 2, 3})
Expected Output:True

Time Complexity: O(len(set))

Related Methods: issuperset()

Remember: Can be written using <= operator.
issuperset
Return: bool

Check if the set contains all elements of another set.

Used In: Schema matching validations.

Syntax signature:set.issuperset(other)
Code snippet:
python
{1, 2, 3}.issuperset({1, 2})
Expected Output:True

Time Complexity: O(len(other))

Related Methods: issubset()

Remember: Can be written using >= operator.