a_listUsing
a_listfroma_list.py([1, "4", 9, "a", 0, 4]), write a list comprehension that finds the string elements made only of digits (e.isdigit()), converts each tointwithint(e), and squares it. The filter must reject"a"beforeint()receives it.isdigit()is astrmethod, anda_listalso holds integers, so the filter must testisinstance(e, str)before calling it.
List
Comprehensions shows the trailing if clause
that filters elements before the output expression runs. Combine
two tests in that clause with and. The
and operator stops at the first false operand, so
the second test runs only on the elements the first one
accepted.
your comprehension prints a one-element list holding
16, and uv run ty check passes on your
file. If you swap the two tests in the filter, ty
reports missing-union-attribute on
isdigit, which confirms that the order of the tests
matters.
If you test e.isdigit() alone, or before the
type test, the comprehension raises an
AttributeError at the first element, since the
integer 1 has no isdigit() method. The
type checker flags the call before the program runs.
ty reports missing-union-attribute,
because e is int | str at that point.
The solution tests isinstance(e, str) first, so
and calls isdigit() only on
strings.
# exercise_1.py
a_list = [1, "4", 9, "a", 0, 4]
result = [int(e) ** 2 for e in a_list
if isinstance(e, str) and e.isdigit()]
print(result)
#: [16]"a" fails isdigit(), so the
comprehension drops it before int() can raise a
ValueError on it.
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.
2 on
the diagonal instead of 1In
identity_matrix.py, change the comprehension to put2on the diagonal instead of1, without adding a second pass over the result.
Nested Comprehensions builds the matrix with an inner comprehension inside an outer one. The diagonal comes from a conditional expression in the output position, which chooses between two values. Change what that expression yields, and leave the loops alone.
your identity_matrix.py prints
six rows with 2 on the diagonal, from
[2, 0, 0, 0, 0, 0] to
[0, 0, 0, 0, 0, 2], and matrix comes
straight from the comprehension, with no statement that changes
it afterward.
# exercise_2.py
from typing import Final
SIZE: Final[int] = 6
matrix = [[2 if col == row else 0 for col in range(SIZE)]
for row in range(SIZE)]
for row in matrix:
print(row)
#: [2, 0, 0, 0, 0, 0]
#: [0, 2, 0, 0, 0, 0]
#: [0, 0, 2, 0, 0, 0]
#: [0, 0, 0, 2, 0, 0]
#: [0, 0, 0, 0, 2, 0]
#: [0, 0, 0, 0, 0, 2]The one change is the literal in the conditional expression,
from 1 to 2.
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.
"Galahad" to namesIn
dict_comprehension.py, add"Galahad"tonames, then predict which entries the comprehension produces before running it, given thelen(name) > 3filter.
Dictionary
Comprehensions shows a key expression and a value expression
with an if filter at the end. The filter runs on
the loop variable, before either expression runs. The
comprehension builds a dict, which holds one value
per key. A later name whose key matches an earlier one replaces
that entry.
you wrote down the entries before running, and the edited dict_comprehension.py
prints the keys and values you wrote. You can also say why
"Ni" drops out, and what happens to the entry count
when two names upper-case to the same key.
# exercise_3.py
names = ["Arthur", "Lancelot", "Bedevere",
"Ni", "Robin", "Galahad"]
lengths = {name.upper(): len(name)
for name in names if len(name) > 3}
print(sorted(lengths))
#: ['ARTHUR', 'BEDEVERE', 'GALAHAD', 'LANCELOT', 'ROBIN']
print(lengths["GALAHAD"], "NI" in lengths)
#: 7 FalseFilter before building each entry. The
filter tests len(name), and name is
the loop variable, so the filter sees the original name; the
upper-cased key exists only in the output expression.
Keep one value per key. Two names that
upper-case to the same string collide. The comprehension builds
a dict, so the later value replaces the earlier one
under the shared key. Adding "robin" alongside
"Robin" produces one 'ROBIN' entry,
not two. Its value comes from the name that appears later in the
list.
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.
set_comprehension.pyIn
set_comprehension.py, drop theif len(name) > 1filter, and predict how many entriesuniqueholds before running it. Explain why"J"does not collide with"JOHN".
Set Comprehensions normalizes each name and lets the set discard repeats. Work out the normalized form of every name, including the one-character name, and count the distinct results. Two entries collide only if their normalized strings are equal.
you predicted the count before running, and the edited set_comprehension.py, with
a print(len(unique)) added, prints that number. You
can also say what "J" becomes after
capitalize(), and why that string differs from what
"JOHN" becomes.
# exercise_4.py
names = ["Bob", "JOHN", "alice", "bob", "ALICE", "J", "Bob"]
unique = {name.capitalize() for name in names}
print(len(unique))
#: 4
print(sorted(unique))
#: ['Alice', 'Bob', 'J', 'John']Collapse the repeats. The set holds four
entries, one more than the filtered version. The seven names
normalize to Bob, John,
Alice, Bob, Alice,
J, Bob. A set keeps one of each, so
the duplicates and the case variants collapse to
Bob, John, and Alice.
J makes the fourth.
Change the case and keep every character.
"J" stays apart from "JOHN" because
the normalization changes case and keeps every character.
"J" stays "J", because
capitalize() lower-cases every character after the
first, and "J" has one character.
The filter exists to drop the initial "J" as
noise. Removing the filter shows what the set does on its own.
The set collapses exact duplicates of the normalized form, so a
"J" that abbreviates "John" stays a
separate entry.
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.
comprehension_side_effects.pybuilds a list ofNones. Write a version that keeps the printing but produces a list the caller can use, then say whether a comprehension or aforloop is the right shape for it.
Comprehensions Build, Loops Execute explains why a comprehension is for the collection it builds and a loop is for its side effects. Write a small function that does the printing and returns a value, then call it in the output expression. To decide the shape, ask whether anyone uses the resulting list.
# The shape of exercise_5.py
def show(n: int) -> str:
...your version prints item 1 through
item 3, then a list of three usable values in place
of [None, None, None], and
uv run ty check passes on your file. You can also
name the shape, comprehension or for loop, that
suits each version, and the question that decides it.
The solution returns the line it printed, so the comprehension collects strings.
# exercise_5.py
def show(n: int) -> str:
line = f"item {n}"
print(line)
return line
lines = [show(n) for n in [1, 2, 3]]
#: item 1
#: item 2
#: item 3
print(lines)
#: ['item 1', 'item 2', 'item 3']
for n in [1, 2, 3]: # Printing alone stays a loop
print(f"item {n}")
#: item 1
#: item 2
#: item 3If you leave return line out of
show(), the function returns None
implicitly. lines then prints as
[None, None, None], the list comprehension_side_effects.py
builds. ty reports the omission as
invalid-implicit-return, because
show() declares a str return type.
Give the output expression a value. The
original comprehension collects print()’s return
value, which is always None. The resulting list
holds three Nones. The brackets tell the reader
that the list matters, but the printing is the purpose. When the
output expression calls a function that returns a useful value,
the list holds those values instead of Nones and
becomes the point. A caller can check the strings in
lines, write them to a file, or join them.
Choose the shape by the result. The right
shape depends on whether you want the list. Here the
comprehension is correct, because lines is the
point and the printing is incidental. The for loop
at the end suits comprehension_side_effects.py,
where printing is the purpose. The chapter’s rule decides both
cases: use a comprehension when you want the collection it
produces, and a loop when you want the side effect alone.
A comprehension whose output expression has a side effect
still deserves a second look when its list is useful.
show() does two jobs. A reader must open it to
learn that one of them is printing.
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.
In
unpacking_comprehensions.py, add a fourth entry{"a": 5, "c": 9}todictsand predict what{**d for d in dicts}produces before running it, paying attention to which value wins for the key"a".
Unpacking
in Comprehensions shows **d in the output
expression of a dict comprehension, merging each d
in the order the loop produces it. A repeated key keeps the
value written last. A key keeps the position of its first
insertion, which decides the print order.
you wrote the whole printed dictionary, keys in order, before
running, and the edited unpacking_comprehensions.py
prints what you wrote. You can also say where the key
"a" gets its value and where it gets its
position.
# exercise_6.py
dicts = [{"a": 1}, {"b": 2}, {"a": 3}, {"a": 5, "c": 9}]
print({**d for d in dicts})
#: {'a': 5, 'b': 2, 'c': 9}** merges the dictionaries in iteration order.
When the same key appears more than once, the value from the
later dictionary overwrites the earlier value.
The key "a" takes its value from the last
dictionary but its position from the first. The result orders
keys by first insertion, so "a" prints first, ahead
of "b" and "c".
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.
any()
before sum()In
spent_generator.py, move theany()line above thesum()line. Predict all three printed values before running it, remembering thatany()stops when it finds a match.
A
Generator Expression Runs Once shows consumers sharing one
generator, each taking what remains. any() stops at
its first true value, so the generator keeps its position when
any() returns. Trace which values each later
consumer receives.
you wrote down all three printed values before running, and
the edited spent_generator.py prints
them in that order. You can also name the first value
sum() receives and say why it differs from the
chapter’s run.
# exercise_7.py
nums = (n for n in range(10))
print(any(n == 5 for n in nums))
#: True
print(sum(n * n for n in nums))
#: 230
print(list(nums))
#: []any() pulls values until one matches, so it
consumes 0 through 5 and reports
True. Stopping there leaves the generator part-way
through, not empty. sum() continues from
6 and adds 36 + 49 + 64 + 81, giving
230 of the full range’s 285. By then
sum() has consumed every value, so
list() returns an empty list.
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.
In
genexp_timing.py, turn the generator expression into a list comprehension and name the resultbuilt. Predict the three printed lines, and their order, before running it. Explain which value offactorthe result uses.
The
Gap Between Creation and Consumption shows a generator
expression delaying its work until a consumer pulls values. With
square brackets, the comprehension runs to completion at the
line where it appears. Ask when the comprehension calls
source() and when it reads factor,
then compare with the print order.
# The shape of exercise_8.py
def source() -> list[int]:
...your edited genexp_timing.py builds
built with a list comprehension and prints its
three lines in the order you predicted. You can also say which
value of factor, 2 or 10,
the products use, and at which line the comprehension reads
it.
# exercise_8.py
def source() -> list[int]:
print("source() called")
return [1, 2, 3]
factor = 2
built = [n * factor for n in source()]
#: source() called
print("list created")
#: list created
factor = 10
print(built)
#: [2, 4, 6]The lines print in the same order as in genexp_timing.py, because
the generator expression there also calls source()
on the line where it appears. exercise_8.py renames
the second message to list created.
Compute the products eagerly. The printed
list changes from [10, 20, 30] to
[2, 4, 6], because a list comprehension does all
its work on the line where it appears. The later
factor = 10 leaves built as it was,
because built holds computed numbers rather than
code that reads factor.
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.
In
walrus_filter.py, makecube_if_even()append each argument it receives to a list, then add a second comprehension that produces the same cubes without the walrus. Predict how many calls each comprehension makes overrange(6), and which arguments the second one passes twice.
Scope
and the Walrus Operator shows the filter and the output
expression sharing one call through y. Without the
walrus, the filter calls cube_if_even() for every
element. The output expression calls it again for each element
that passes. Count the elements that pass the filter, and add
that number to the length of data.
# The shape of exercise_9.py
def cube_if_even(n: int) -> int | None:
...both comprehensions print [0, 8, 64], and the
recorded arguments match the call count you predicted for each
one. You can also name the arguments the second comprehension
passes twice and say why it passes the rest once.
# exercise_9.py
calls: list[int] = []
def cube_if_even(n: int) -> int | None:
calls.append(n)
return n ** 3 if n % 2 == 0 else None
data = range(6)
cubes = [
y for x in data if (y := cube_if_even(x)) is not None
]
print(cubes, len(calls))
#: [0, 8, 64] 6
calls.clear()
again = [
cube_if_even(x) for x in data
if cube_if_even(x) is not None
]
print(again, len(calls))
#: [0, 8, 64] 9
print(calls)
#: [0, 0, 1, 2, 2, 3, 4, 4, 5]One call per element with the walrus. The
output expression reads each result back from y
instead of calling again.
A second call for each element that passes.
again calls cube_if_even() in the
filter for all six values and a second time in the output
expression for the three that pass. calls records
the order. 0, 2, and 4
each appear twice in a row, once for the filter and then once
for the output expression. 1, 3, and
5 appear once, because the filter rejects them
before the output expression runs. Six plus three makes nine
calls for the same three cubes.
cube_if_even() returns in a moment, but for a
function that reads a file or queries a server, the walrus
halves the calls for every element that passes.
Before you move on, name what separates your attempt from this one: a different design, a detail, or an equivalent alternative.