7.4. Sets#

Hide code cell source

import sys
from pathlib import Path

current = Path.cwd()
for parent in [current, *current.parents]:
    if (parent / '_config.yml').exists():
        project_root = parent  # ← Add project root, not chapters
        break
else:
    project_root = Path.cwd().parent.parent

sys.path.insert(0, str(project_root))

from shared import thinkpython, diagram, jupyturtle, structshape
from shared.download import download

# Register as top-level modules so direct imports work in subsequent cells
sys.modules['thinkpython'] = thinkpython
sys.modules['diagram'] = diagram
sys.modules['jupyturtle'] = jupyturtle

A set holds unique values with no order, which makes it the right tool for removing duplicates, testing membership, and comparing collections. This section covers what sets are, which values a set can hold, how to create and use sets, and the set operations union, intersection, and difference.

7.4.1. What Sets Are#

A set is an unordered collection of unique values. Unlike lists or tuples, sets do not allow duplicates, and the elements have no guaranteed order.

Here below is an example of a set. Note that it uses curly braces just like dictionaries.

s = {1, 2, 3, 3, 2}
print(s)                ### {1, 2, 3}
{1, 2, 3}

Sets are useful when you care about membership: whether something is in a collection; rather than order or position.

The most common use cases are:

  • Removing duplicates from a list

  • Checking membership quickly

  • Comparing two collections (what’s shared, what’s different)

For sets, the four key properties that make sets different from other data structures:

  1. Unordered: Elements have no guaranteed position. There is no first or last element.

  2. Unique: Duplicates are automatically removed.

  3. Mutable: You can add and remove elements.

  4. Hashable elements only: Every element must be hashable. Strings and numbers work; tuples work only when every element is hashable. Lists, dictionaries, and sets do not work.

### 1. Sets are unordered, so the order of elements is not guaranteed.
{3, 1, 2} == {1, 2, 3}  # True
True
### 2. Sets do not allow duplicate elements, so duplicates are automatically removed.
{1, 2, 2, 3, 3}         # {1, 2, 3}
{1, 2, 3}
### 3. Sets are mutable, so you can add or remove elements after the set is created.
s = {1, 2, 3}
s.add(4)                # works
s.remove(2)             # works
s
{1, 3, 4}
%%expect TypeError
### 4. Sets can contain only hashable elements, so you cannot include lists or other sets as elements.
{1, "hello", (1, 2)}    # valid
{[1, 2], [3, 4]}        # TypeError
TypeError: unhashable type: 'list'
### Exercise: Deduplicate Course Codes *(basic)*
# The list below contains repeated course codes.
# 1. Build a set of unique codes.
# 2. Print the unique set.
# 3. Print how many distinct course codes appear.
course_codes = ["PY", "DS", "PY", "SQL", "DS", "AI"]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
course_codes = ["PY", "DS", "PY", "SQL", "DS", "AI"]
unique_codes = set(course_codes)
print(unique_codes)
print(len(unique_codes))
{'SQL', 'AI', 'PY', 'DS'}
4

7.4.1.1. Choosing the Right Structure#

Need

Use

Ordered collection, duplicates allowed

list

Fixed data, multiple return values

tuple

Key-value lookup

dict

Unique items, membership testing

set

Hashable set (dict key, nested set)

frozenset

7.4.1.2. Hashable vs. Mutable Values#

Mutability describes whether an object can change. Hashability describes whether it has a stable hash and equality behavior suitable for dictionary keys and set elements. These are distinct properties.

  • Hashable means an object has a hash value that stays stable during its lifetime, and equal objects have equal hashes.

  • The built-in mutable containers list, dict, and set are not hashable. Some user-defined mutable objects are hashable by identity.

  • Immutable objects are often hashable (for example: int, float, str, tuple), but for tuples, all nested elements must also be hashable.

  • For hash-table types: dict requires hashable keys, and set requires hashable elements.

7.4.1.3. Hashability#

Let us compare set, list, and dict and this time focus on hashability.

Feature

Set

List

Dict

Ordered

No

Yes

Yes (insertion order)

Duplicates

No

Yes

Keys: No, Values: Yes

Mutable

Yes

Yes

Yes

Allows only hashable elements

Yes

No

Keys only

Lookup speed

Avg O(1)

O(n)

Avg O(1)

Use case

Unique items, membership

Ordered items

Key-value pairs

In computer science, hashable means that an object can be converted into a fixed-size integer (called a hash value or hash code) via a hash function. This hash value is used to quickly locate the object in data structures like hash tables, dictionaries, and sets. For something to be hashable, it generally needs to satisfy two properties:

  • Consistency: the hash value must not change during the object’s lifetime. Built-in lists, dictionaries, and sets are unhashable. Mutability alone does not determine hashability for every Python object.

  • Equality consistency: if two objects are considered equal (a == b), they must have the same hash value. The reverse isn’t required; two different objects can share a hash (called a collision), but equal objects must hash identically.

Hash-based data structures like hash maps and hash sets rely on hash values to place and find items in O(1) average time. If you want to use an object as a dictionary key or store it in a set, it needs to be hashable.

In summary, hashable means you can be consistently fingerprinted with a number, which is the prerequisite for being usable as a key in fast lookup structures.

key     →  hash function    →  index  →  bucket in array
"cat"   →  hash("cat")      →  42     →  array[42]
print(hash("cat"))           ### -9223372036854775808
print(hash("dog"))           ### -867955804616931492
print(hash("cat"))           ### -9223372036854775808
-6557399113358495866
2232670265565077654
-6557399113358495866

7.4.2. Creating Sets and Testing Membership#

A set is created using curly braces {} with comma-separated values, or by using the set() constructor.

Unlike lists, sets are unordered and do not allow duplicates - if you add the same value twice, it only appears once. This makes sets useful for storing unique items. One important gotcha: an empty {} creates a dict, not a set - you must use set() for an empty set.

s = {1, 2, 3, 3}       # duplicate 3 is ignored
print(s)               # {1, 2, 3}

s = set([1, 2, 2, 3])  # from a list
print(s)               # {1, 2, 3}

s = set()              # empty set
d = {}                 # this is a dict, not a set!
{1, 2, 3}
{1, 2, 3}

set elements must be hashable. Since sets use hashing internally to enforce uniqueness and enable fast lookup, all elements must be hashable. This means you can store integers, floats, strings, and tuples of hashable elements in a set, but not lists or dictionaries.

%%expect TypeError
# Valid
s = {1, "hello", (1, 2)}

# Invalid
s = {[1, 2], [3, 4]}    # TypeError: unhashable type: 'list'
TypeError: unhashable type: 'list'

Similarly, a tuple is hashable only if all of its elements are hashable.

print(hash((1, 2)))          # ok
print(hash((1, (2, 3))))     # ok

try:
    hash((1, [2, 3]))        # list inside tuple => unhashable
except TypeError as e:
    print(e)
-3550055125485641917
7267574591690527098
unhashable type: 'list'

7.4.2.1. Accessing and Iterating#

Sets have no indexing or positional access. Because sets are unordered, there’s no concept of “first” or “second” element, so index access makes no sense.

Compared with list and dictionary, dict is the only one with a built-in safe getter. A list requires a guard clause.

List

Dict

Set

Access

by index l[i]

by key d[key]

no direct access

Safe get

l[i] if i < len(l) else default

d.get(key, default)

val if val in s else default

Built-in safe method

none

.get()

none

%%expect IndexError
d = {'x': 1, 'y': 2}
d.get('z', 'not found')                 # 'not found' — no error

l = [10, 20, 30]
l[5]                                  # IndexError!   ### uncomment to see the error
IndexError: list index out of range

For list, a safe way to handle a possible out-of-range error is to use a guard clause.

l = [10, 20, 30]

result = l[5] if 5 < len(l) else None   # None — safe
print(result)                           # None
None

To work with elements in a set, you can:

Method

Example

Notes

Membership check

3 in s

True/False only, no error

Iterate

for x in s:

no guaranteed order

Convert to list

list(s)[0]

order not guaranteed

s.pop()

s.pop()

returns an arbitrary element

### check membership

if "apple" in s:
    print("found")
### iterate over a set
for element in s:
    print(element)
1
(1, 2)
hello
### conversion to list
l = list(s)
print(l)
[1, (1, 2), 'hello']
### s.pop() removes and returns an arbitrary element from the set. If the set is empty, it raises a KeyError.
s = {1, 2, 3}
s.pop()
1
### Exercise: Membership-Heavy Search *(applied)*
# Use a set when you need many membership checks.
# 1. Convert allowed_users to a set.
# 2. Print each login name that is allowed.
allowed_users = ["maya", "li", "ana", "sam"]
login_attempts = ["eli", "maya", "sam", "zoe", "ana"]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
allowed_users = ["maya", "li", "ana", "sam"]
login_attempts = ["eli", "maya", "sam", "zoe", "ana"]
allowed = set(allowed_users)

for name in login_attempts:
    if name in allowed:
        print(name)
maya
sam
ana

7.4.3. Set Algebra#

Sets support mathematical operations that make it easy to compare two collections. Each operation has both an operator shorthand and a method form which produce the same result:

Operation

Operator

Method

Description

Union

a | b

a.union(b)

All elements from both sets

Intersection

a & b

a.intersection(b)

Only elements common to both sets

Difference

a - b

a.difference(b)

Elements in a but not in b

Symmetric Difference

a ^ b

a.symmetric_difference(b)

Elements in either set, but not in both

Read each operation as a membership rule. For a = {1, 2, 3} and b = {3, 4, 5}, the shared element is 3:

Element

In a?

In b?

Union: either

Intersection: both

a - b: a only

Symmetric difference: exactly one

1

Yes

No

Keep

Omit

Keep

Keep

2

Yes

No

Keep

Omit

Keep

Keep

3

Yes

Yes

Keep

Keep

Omit

Omit

4

No

Yes

Keep

Omit

Omit

Keep

5

No

Yes

Keep

Omit

Omit

Keep

Difference has a direction: a - b keeps 1 and 2, while b - a keeps 4 and 5. Predict each result from the table before running the examples. These algebra operations create new sets and leave the inputs unchanged.

a = {1, 2, 3}
b = {3, 4, 5}

Note that, operator forms (|, &, -, ^) require set operands. Method forms are more flexible and can take any iterable.

print(a.union([3, 4, 5]))    # works: method accepts iterable

try:
    a | [3, 4, 5]            # TypeError: right side is a list
except TypeError as e:
    print(e)
{1, 2, 3, 4, 5}
unsupported operand type(s) for |: 'set' and 'list'

7.4.3.1. Union#

All elements from both sets

a | b        # {1, 2, 3, 4, 5}
a.union(b)   # same
{1, 2, 3, 4, 5}

7.4.3.2. Intersection#

Only elements in both sets.

a & b              # {3}
a.intersection(b)  # same
{3}

7.4.3.3. Difference#

Elements in a but not in b

a - b              # {1, 2}
a.difference(b)    # same
{1, 2}

7.4.3.4. Symmetric Difference#

Elements in either set, but not both

a ^ b                       # {1, 2, 4, 5}
a.symmetric_difference(b)   # same
{1, 2, 4, 5}
### Exercise: Basic Set Operations
# 1. Create A = {1, 2, 3, 4} and B = {3, 4, 5, 6}.
# 2. Print A | B, A & B, A - B, and A ^ B.
### Your code starts here.




### Your code ends here.

Hide code cell source

### Solution
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print("Union:", A | B)
print("Intersection:", A & B)
print("Difference A-B:", A - B)
print("Symmetric Difference:", A ^ B)
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference A-B: {1, 2}
Symmetric Difference: {1, 2, 5, 6}

7.4.4. Mutating Sets Safely#

Python sets have methods for adding, removing, and checking elements.

Method

Description

.add(x)

Adds a single element

.update(iterable)

Adds multiple elements

.copy()

Returns a shallow copy of the set

.remove(x)

Removes element, raises KeyError if missing

.discard(x)

Removes element, silent if missing

.pop()

Removes and returns an arbitrary element

.clear()

Empties the set

.issubset(s)

Returns True if all elements are in s

.issuperset(s)

Returns True if set contains all of s

.isdisjoint(s)

Returns True if no elements in common

7.4.4.1. Mutability#

For the property of mutability, let’s have a look over the general data structures. For in-place change methods for the common data structures, here is a comparison table. Tuple and strings are immutable, so there’s no methods available for the purpose.

Method

List

Dict

Set

Tuple

String

.append(x)

✓

.insert(i, x)

✓

.extend(iter)

✓

.update(other)

✓

✓

.remove(x)

✓

✓

.pop()

✓

✓

✓

.popitem()

✓

.add(x)

✓

.discard(x)

✓

.sort()

✓

.reverse()

✓

.setdefault(k, v)

✓

.clear()

✓

✓

✓

.intersection_update(t)

✓

.difference_update(t)

✓

.symmetric_difference_update(t)

✓

7.4.4.2. In-place Set Operations#

The algebra above creates new sets. The corresponding update methods change the existing set and return None. This return-value rule applies to these update methods; methods such as pop() mutate a collection and return a removed element.

New set

Change the original set

a.union(b)

a.update(b)

a.intersection(b)

a.intersection_update(b)

a.difference(b)

a.difference_update(b)

a.symmetric_difference(b)

a.symmetric_difference_update(b)

a = {1, 2, 3}
b = {3, 4, 5}
combined = a.union(b)
print('New union:', sorted(combined))
print('Original unchanged:', sorted(a))
result = a.update(b)
print('After update:', sorted(a))
print('Update returned:', result)

a = {1, 2, 3}
a.intersection_update(b)
print('intersection_update:', sorted(a))
a = {1, 2, 3}
a.difference_update(b)
print('difference_update:', sorted(a))
a = {1, 2, 3}
a.symmetric_difference_update(b)
print('symmetric_difference_update:', sorted(a))
New union: [1, 2, 3, 4, 5]
Original unchanged: [1, 2, 3]
After update: [1, 2, 3, 4, 5]
Update returned: None
intersection_update: [3]
difference_update: [1, 2]
symmetric_difference_update: [1, 2, 4, 5]

7.4.4.3. Adding elements:#

  • add()

  • update(): adds multiple elements, similar to dict.update().

s = {1, 2, 3}
s.add(4)            # {1, 2, 3, 4}
s.update([5, 6])    # {1, 2, 3, 4, 5, 6}
s
{1, 2, 3, 4, 5, 6}

7.4.4.4. Copy vs aliasing#

Sets are mutable, so assignment shares the same object. Use .copy() when you need an independent set.

a = {1, 2}
b = a               # alias (same object)
c = a.copy()        # separate object

a.add(3)
print("a:", a)
print("b:", b)
print("c:", c)
a: {1, 2, 3}
b: {1, 2, 3}
c: {1, 2}

7.4.4.5. Removing elements:#

  • remove()

  • discard()

  • pop()

  • clear()

s = {1, 2, 3}

s.remove(3)     # raises KeyError if not found
print(s)        # {1, 2}

s.discard(3)    # silent if not found — safer
print(s)        # {1, 2}

s.pop()         # removes and returns an arbitrary element
print(s)        # which item is removed is arbitrary

s.clear()       # empties the set
print(s)        # set()
{1, 2}
{1, 2}
{2}
set()

If you use remove() in a for loop for a set, you may run into an error because Python creates an internal iterator that tracks position inside the set. The moment you call s.remove(), the set’s structure changes and the iterator breaks and raises an error.

The solution to this problem is to iterate over a copy or just use a while loop.

%%expect RuntimeError
s = {1, 2, 3,}
for item in s:
    s.remove(item)
    print(f"Processing {item}")
Processing 1
RuntimeError: Set changed size during iteration
### iterate through copy of the set while modifying the original

for item in s.copy():   # iterate over copy
    s.remove(item)      # modify original safely
    print(f"Processing {item}")
s
Processing 2
Processing 3
set()
# process all items, consuming the set

s = {1, 2, 3}
while s:
    item = s.pop()
    print(f"Processing {item}")
# s is now empty
Processing 1
Processing 2
Processing 3

s.pop() is useful when you want to process and consume a set one element at a time, and you don’t care which element you get. This behavior is different from pop() in list and dict.

list.pop()

dict.pop()

set.pop()

Removes

by index

by key

arbitrary element

Returns

removed value

removed value

removed value

Default arg

index (default -1)

key + optional default

none

If not found

IndexError

KeyError (or default)

KeyError

Predictable?

yes (by index)

yes (by key)

no (arbitrary item)

7.4.4.6. Checking membership:#

The in and not in operators test whether a value is in a set. Because sets are built on hashing, this check stays fast even for very large sets.

s = {1, 2, 3}

print(3 in s)         # True
print(3 not in s)     # False
True
False

7.4.4.7. Subset & Superset:#

a = {1, 2}
b = {1, 2, 3}

a.issubset(b)    # True — all of a is in b
b.issuperset(a)  # True — b contains all of a
a.isdisjoint(b)  # False — they share elements
False

Operator equivalents for subset/superset are often used in real code:

a = {1, 2}
b = {1, 2, 3}

print(a < b)    # proper subset
print(a <= b)   # subset
print(b > a)    # proper superset
print(b >= a)   # superset
print(a <= a)   # True (same set is subset of itself)
print(a < a)    # False (not a proper subset)
True
True
True
True
True
False
### Exercise: Update and Membership Check
# 1. Start with fruits = {"apple", "banana"}.
# 2. Add "cherry" and update with ["banana", "mango"].
# 3. Remove "apple" safely.
# 4. Print whether "mango" is in fruits.
# 5. Print whether {"banana", "cherry"} is a subset of fruits.
### Your code starts here.





### Your code ends here.

Hide code cell source

### Solution
fruits = {"apple", "banana"}
fruits.add("cherry")
fruits.update(["banana", "mango"])
fruits.discard("apple")

print("mango" in fruits)
print({"banana", "cherry"}.issubset(fruits))
True
True

7.4.5. Frozensets, Hashability, and Performance#

A frozenset is an immutable version of a set. Once created, elements cannot be added or removed. This is useful when, for example, you need to protect data from modification, to use set as dict key or set element, or for graph edges and combinations.

%%expect AttributeError
fs = frozenset({1, 2, 3})
fs.add(4)     # AttributeError — frozensets are immutable
AttributeError: 'frozenset' object has no attribute 'add'

7.4.5.1. Creating a frozenset#

The syntax for creating a frozenset is:

frozenset()            # empty frozenset
frozenset(iterable)    # from any iterable (list, set, string, etc.)
fs = frozenset([1, 2, 3])
print(fs)                   # frozenset({1, 2, 3})
fs = frozenset("hello")
print(fs)                   # element order may vary
frozenset({1, 2, 3})
frozenset({'e', 'o', 'h', 'l'})

7.4.5.2. Why use a frozenset?#

A frozenset is immutable and contains only hashable elements. It is itself hashable, so it can be:

  • used as a dictionary key, or

  • stored inside another set.

# As a dict key
d = {frozenset({1, 2}): "pair"}

# Inside a set
s = {frozenset({1, 2}), frozenset({3, 4})}

Feature

set

frozenset

Mutable

Yes

No

Hashable

No

Yes

Can be dict key

No

Yes

Set operations

All

Non-mutating only

7.4.5.3. Set Performance and Membership#

When you store a key in a dictionary or an element in a set, Python calls hash() on it to compute an integer, then uses that integer to determine where to store the value in memory. This is called a hash table.

The benefit is that lookups are usually O(1) on average: Python jumps directly to a bucket via hash instead of scanning the whole collection. In the worst case (many collisions), lookup can degrade toward O(n).

# A missing target forces a list to check every element.
print('pear' in ['apple', 'banana', 'cherry'])
# A set uses hashing for average O(1) membership.
print('pear' in {'apple', 'banana', 'cherry'})
False
False

O(n) and O(1) come from Big O notation, which describes how the speed of an operation scales with the size of the data. The full treatment of Big O comes in the Algorithms chapter, where sorting and searching will be covered.

For now, just know that:

  • Average O(1): hash-table lookups in dict/set are constant time on average, because Python uses the hash to jump to a bucket.

  • Worst-case O(n): if many keys collide into the same bucket, Python may need to scan multiple entries.

  • O(n): linear time. The operation gets slower as the collection grows. A list search is O(n) because Python checks each element one by one until it finds a match:

d = {'apple': 1, 'banana': 2, 'cherry': 3}
print(d['apple'])  # Average O(1) lookup; elapsed time need not be identical at every size.
1
"apple" in ["apple", "banana", "cherry"]  # checks up to n items
True

Big O describes growth in work, not an exact duration. Hashing and equality checks also have costs, so the comparison below uses simple integer keys to focus on collection size.

You already saw this in the dictionary section, and the same idea applies here:

  • O(1) means near-constant lookup time on average (key in dict, item in set).

  • O(n) means linear time, where Python may scan through many elements (item in list).

So for membership checks, sets are usually much faster than lists as data grows.

7.4.5.4. Measure Growth and Construction Cost#

Section 7.3 compared repeated list and dictionary lookups. Add sets to that comparison at several sizes. The missing target makes the list scan all entries. Each reported lookup time is the best of three batches, divided by the number of repetitions.

Build the lookup collections outside the lookup timing, then measure construction separately. These measurements illustrate a tradeoff: a set takes work to build, but can be reused across many searches. Timings vary by machine and do not prove a complexity bound.

from timeit import repeat

print('Size    list us    dict us     set us    build set us')
for n in (100, 1_000, 10_000):
    values = list(range(n))
    lookup = dict.fromkeys(values)
    members = set(values)
    target = -1
    repetitions = 1_000
    def per_lookup(collection):
        seconds = min(repeat(lambda: target in collection, number=repetitions, repeat=3))
        return seconds / repetitions * 1e6
    build_seconds = min(repeat(lambda: set(values), number=100, repeat=3)) / 100
    print(f'{n:5d} {per_lookup(values):10.3f} {per_lookup(lookup):10.3f} '
          f'{per_lookup(members):10.3f} {build_seconds * 1e6:15.3f}')
Size    list us    dict us     set us    build set us
  100      0.919      0.042      0.040           1.630
 1000      9.317      0.041      0.039          12.107
10000     93.149      0.042      0.040         136.536

7.4.5.5. Choose a Set or a Dictionary for Word Lookup#

For a reverse-word search, membership alone is enough: a set stores which words exist. A dictionary is useful when each word must also map to information such as its frequency. Build either lookup once and reuse it. The loop below examines each distinct word once; its output is a list of directed matches, so both directions of a pair appear.

words = ['stressed', 'desserts', 'cat', 'tac', 'python', 'cat']
word_set = set(words)
matches = [(word, word[::-1]) for word in sorted(word_set) if word[::-1] in word_set]
print('Reverse matches:', matches)

from collections import Counter
word_counts = Counter(words)
print('cat exists:', 'cat' in word_set)
print('cat frequency:', word_counts['cat'])
Reverse matches: [('cat', 'tac'), ('desserts', 'stressed'), ('stressed', 'desserts'), ('tac', 'cat')]
cat exists: True
cat frequency: 2
### Exercise: Choose List or Set *(applied)*
# For each task, choose list or set and create the data structure.
# 1. Keep every score in order, including duplicates: 88, 92, 88, 75.
# 2. Keep only the unique tags from: "python", "data", "python", "text".
# 3. Check whether "data" appears in the tags.
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
scores = [88, 92, 88, 75]
tags = {"python", "data", "python", "text"}
print(scores)
print(tags)
print("data" in tags)
[88, 92, 88, 75]
{'python', 'text', 'data'}
True

7.4.5.6. Set Comprehensions#

A set comprehension builds a set from an expression, using the same syntax as a list comprehension but with curly braces {}. Because the result is a set, duplicates are automatically discarded.

The syntax for creating sets is:

{expression for item in iterable}
{expression for item in iterable if condition}
# Create a set of squares
square_set = {x**2 for x in range(-3, 4)}
print("Square set:", square_set)

# Remove duplicates and transform
sentence = "the quick brown fox jumps over the lazy dog"
unique_lengths = {len(word) for word in sentence.split()}
print("Unique word lengths:", unique_lengths)

# Filter vowels
vowels = {char.lower() for char in "Hello World" if char.lower() in 'aeiou'}
print("Vowels found:", vowels)
Square set: {0, 9, 4, 1}
Unique word lengths: {3, 4, 5}
Vowels found: {'e', 'o'}
### Exercise: Set Comprehension Practice
# 1. From nums = [1, 2, 2, 3, 4, 4, 5], build a set of squared values.
# 2. From text = "apple banana apple cherry", build a set of unique word lengths.
# 3. Print both sets.
### Your code starts here.




### Your code ends here.

Hide code cell source

### Solution
nums = [1, 2, 2, 3, 4, 4, 5]
squares = {n**2 for n in nums}

text = "apple banana apple cherry"
lengths = {len(word) for word in text.split()}

print("Squares:", squares)
print("Unique lengths:", lengths)
Squares: {1, 4, 9, 16, 25}
Unique lengths: {5, 6}

7.4.5.7. Comparing with dict Comprehension#

Set and dictionary comprehensions both use curly braces, so they are easy to confuse. The difference is the colon: {expr for ...} builds a set, and {key: value for ...} builds a dictionary.

# Set comprehension — curly braces, no colon
{x**2 for x in range(5)}

# Dict comprehension — curly braces WITH colon
{x: x**2 for x in range(5)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}