AI Skill Report Card

Using Python Sets

B68·Aug 12, 2026·Source: Web
13 / 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.
12 / 15
  1. Choose the right type

    • set — need to add/remove elements later.
    • frozenset — need immutability (e.g., as dict key, set element, or for safety).
  2. Construct

    • Literal: {1, 2, 3} (note: {} is an empty dict, use set() for empty set).
    • From iterable: set(iterable), frozenset(iterable).
    • Set comprehension: {x*2 for x in range(5)}.
  3. 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.
  4. 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) raises KeyError if absent; discard(x) does not.
  5. Comparisons

    • <= / issubset(), >= / issuperset(), < (proper subset), > (proper superset).
    • isdisjoint() checks for no common elements without building the intersection.
  6. Verify elements are hashable

    • Set elements must be hashable (immutable types: int, str, tuple of hashables, frozenset). Lists/dicts/sets cannot be elements — use frozenset or tuple instead.
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.
14 / 20

Example 1: Deduplicate while preserving a fast lookup Input:

Python
names = ["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:

Python
a = {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:

Python
groups = {} groups[frozenset({1, 2})] = "pair"

Output:

Python
{frozenset({1, 2}): 'pair'}

Example 4: Symmetric difference for "changed" detection Input:

Python
before = {"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.
  • 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 frozenset for hashable, immutable groupings (dict keys, elements of other sets).
  • Use issubset/issuperset/isdisjoint instead 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 of set(map(...)) for clarity.
  • 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 — prefer discard() to avoid KeyError, 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 *_update in-place variants — assigning the result is required for non-mutating methods.
0
Grade BAI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
13/15
Workflow
12/15
Examples
14/20
Completeness
15/20
Format
14/15
Conciseness
13/15