Background:
Sets (set) were introduced in Python as a separate built-in type starting from Python 2.4 (before that, they were implemented as an external module). They allow for efficient storage of unique, unordered elements and support a variety of standard set theory operations: union, intersection, difference, symmetric difference, and subset checks.
Problem:
Without a set, one has to use lists, which leads to inefficient searching for unique elements, slowing down algorithms that require many membership checks. A set is implemented through a hash table, so all search, add, and delete operations are performed in amortized O(1) time. However, this structure can lead to unexpected issues—such as losing the order of elements, restrictions on element types (they must be immutable and hashable), and misunderstanding the nuances of set comparison and their interaction with other structures.
Solution:
Use a set when you need to store only unique elements and perform set theory operations on them. Remember that elements must be hashable (for example, numbers, strings, tuples, but not lists or dicts). A rich set of built-in methods is provided for working with sets (add, remove, union, intersection, difference, issubset, etc.).
Code example:
s1 = {1, 2, 3, 4} s2 = {3, 4, 5} print(s1 | s2) # {1, 2, 3, 4, 5} (union) print(s1 & s2) # {3, 4} (intersection) print(s1 - s2) # {1, 2} (difference) print(3 in s1) # True (membership check)
Key features:
Can you add mutable types (e.g., lists) to a set?
No, elements of a set must be hashable and immutable. A list or dict cannot be added to a set, and Python will raise TypeError.
s = set() s.add([1, 2, 3]) # TypeError: unhashable type: 'list'
Does a set preserve the order of elements?
No. Since the creation of a set, elements are not guaranteed to be returned in the order they were added, especially when the size of the set changes.
s = {5, 2, 8, 1} print(s) # Order is not defined
What is the difference between set and frozenset, and can frozenset be used as an element of a set?
frozenset is the immutable variant of set. It can be used as an element of another set or as a key in a dict because it is hashable.
fs = frozenset([1, 2, 3]) s = set() s.add(fs) # OK
A developer wanted to store unique elements and chose set without considering the order of their appearance. As a result, part of the business logic that depended on the order of processing stopped working.
Pros:
Cons:
In a task for quickly filtering unique records from a large dataset, a set was chosen because only the fact of uniqueness was important, not the order. Performance increased significantly, and the code became simpler.
Pros:
Cons: