AI Skill Report Card
Using Python Sets
Quick Start13 / 15
Python# Sets: mutable, unordered, unique elements, must be hashable s = {1, 2, 3} fs = frozenset([1, 2, 3]) # immutable, hashable variant # Common operations a = {1, 2, 3} b = {2, 3, 4} a | b # union -> {1, 2, 3, 4} a & b # intersection -> {2, 3} a - b # difference -> {1} a ^ b # symmetric difference -> {1, 4} # Membership test — O(1) average 3 in a # True
Recommendation▾
This topic is largely basic Python knowledge Claude already has; the skill would benefit more from covering performance nuances (e.g., set vs list lookup complexity, memory tradeoffs), advanced use cases (graph algorithms, caching), or real-world debugging scenarios rather than restating language fundamentals.
Workflow12 / 15
-
Choose the right type
set— need to add/remove elements later.frozenset— need immutability (e.g., as dict key, set element, or for safety).
-
Construct
- Literal:
{1, 2, 3}(note:{}is an empty dict, useset()for empty set). - From iterable:
set(iterable),frozenset(iterable). - Set comprehension:
{x*2 for x in range(5)}.
- Literal:
-
Pick operators vs methods
- Operators (
|,&,-,^) require both operands to be sets. - Methods (
union(),intersection(),difference(),symmetric_difference()) accept any iterable. - Use methods when combining a set with a list/generator; use operators between two sets for readability.
- Operators (
-
Mutating vs non-mutating
- Non-mutating:
union,intersection,difference,symmetric_difference,copy. - Mutating (set only):
update,intersection_update,difference_update,symmetric_difference_update,add,remove,discard,pop,clear. remove(x)raisesKeyErrorif absent;discard(x)does not.
- Non-mutating:
-
Comparisons
<=/issubset(),>=/issuperset(),<(proper subset),>(proper superset).isdisjoint()checks for no common elements without building the intersection.
-
Verify elements are hashable
- Set elements must be hashable (immutable types:
int,str,tupleof hashables,frozenset). Lists/dicts/sets cannot be elements — usefrozensetortupleinstead.
- Set elements must be hashable (immutable types:
Recommendation▾
Examples are correct but low-stakes; add a more complex/realistic example such as deduplicating objects by a derived key, or performance comparison demonstrating why sets are chosen over lists.
Examples14 / 20
Example 1: Deduplicate while preserving a fast lookup Input:
Pythonnames = ["alice", "bob", "alice", "carol"] unique = set(names)
Output:
Python{"alice", "bob", "carol"} # order not guaranteed
Example 2: Combine a set with a list using a method (operator would fail) Input:
Pythona = {1, 2, 3} b = [3, 4, 5] a.union(b)
Output:
Python{1, 2, 3, 4, 5}
Note: a | b raises TypeError since b is a list, not a set.
Example 3: Frozenset as a dict key Input:
Pythongroups = {} groups[frozenset({1, 2})] = "pair"
Output:
Python{frozenset({1, 2}): 'pair'}
Example 4: Symmetric difference for "changed" detection Input:
Pythonbefore = {"a", "b", "c"} after = {"b", "c", "d"} before ^ after
Output:
Python{"a", "d"}
Recommendation▾
Consider adding a section on performance characteristics (mentioned in description) with concrete Big-O notes or benchmarks, since the description promises this but the body doesn't substantively deliver it.
Best Practices
- Use
set()for empty sets —{}creates a dict. - Prefer set/frozenset for membership testing over lists when the collection is large or checked repeatedly.
- Use
frozensetfor hashable, immutable groupings (dict keys, elements of other sets). - Use
issubset/issuperset/isdisjointinstead of manually computing intersections when you only need a boolean. - When updating in a loop, use in-place methods (
update,add) to avoid creating new set objects repeatedly. - Use set comprehensions (
{expr for x in iterable}) instead ofset(map(...))for clarity.
Common Pitfalls
- Assuming set order is stable or meaningful — sets are unordered; don't rely on iteration order.
- Using
{}expecting an empty set — it's an empty dict. - Putting mutable objects (lists, dicts, other sets) directly into a set — raises
TypeError: unhashable type. - Using
remove()when the element might not exist — preferdiscard()to avoidKeyError, or catch the exception explicitly. - Applying operators (
|,&,-,^) between a set and a non-set iterable — use the equivalent method instead. - Forgetting that set operations return new sets unless using the
*_updatein-place variants — assigning the result is required for non-mutating methods.