Python Sets
Introduction
A Python set is an unordered, mutable collection of unique elements.
Creating Sets
You can create a set using curly braces {} or the set() constructor.
# Create a set with elements
nums = {1, 2, 3}
# Create from an iterable (removes duplicates automatically)
chars = set("hello")
print(chars) # {'h', 'e', 'l', 'o'}
The Empty Set Trap
You cannot create an empty set using {}. That creates an empty dictionary. You must use set().
empty_dict = {}
print(type(empty_dict)) # <class 'dict'>
empty_set = set()
print(type(empty_set)) # <class 'set'>
Modifying Sets
Because sets are unordered, they do not have append() (which implies adding to the end) or insert() (which requires an index).
Adding Elements
Use add() to insert a single element.
Removing Elements
There are two ways to remove elements:
remove(x): Removes the element, but raises aKeyErrorif it doesn't exist.discard(x): Removes the element if it exists; does nothing if it doesn't.
If you need to remove an arbitrary element, use pop(). It returns a random element from the set (useful in some graph algorithms).
Membership Testing (in)
The primary reason to use a set in an interview is for fast membership testing using the in operator.
Unordered Nature
Sets do not maintain insertion order (unlike modern Python dictionaries).
You cannot access elements by index (nums[0] will raise a TypeError), and iterating over a set yields elements in a seemingly random order based on their hash values.
Time and Space Complexity
- Time Complexity: \(O(1)\) for add, remove, and
in. \(O(N)\) to iterate. - Space Complexity: \(O(N)\) to store \(N\) elements.
Summary
- Use
set()to create an empty set, not{}. - Use
add(),remove(), anddiscard()to modify sets. - Use
infor \(O(1)\) membership testing. - Sets are unordered and do not support indexing.