12.1. Comprehension About
12.1.1. Summary
Loop leaks out values
Syntax:
>>>
... result = [<APPEND> for <VARIABLE> in <ITERABLE>]
12.1.2. Problem
>>> data = ['Mark', 'Melissa', 'Rick']
>>>
>>> result = []
>>> for name in data:
... result.append(name)
>>>
>>> print(result)
['Mark', 'Melissa', 'Rick']
12.1.3. Solution
>>> data = ['Mark', 'Melissa', 'Rick']
>>>
>>> result = [name for name in data]
>>>
>>> print(result)
['Mark', 'Melissa', 'Rick']
12.1.4. Naming
Use shorter variable names
x
is common name
>>> data = ['Mark', 'Melissa', 'Rick']
>>>
>>> result = [name for name in data]
>>> result = [x for x in data]
12.1.5. Scope
Variables leak from loops
Variables do not leak from comprehensions
For loop:
>>> print(x)
Traceback (most recent call last):
NameError: name 'x' is not defined
>>>
>>> data = ['Mark', 'Melissa', 'Rick']
>>>
>>> result = []
>>> for x in data:
... result.append(x)
>>>
>>> print(x)
Rick
Comprehension:
>>> print(x)
Traceback (most recent call last):
NameError: name 'x' is not defined
>>>
>>> data = ['Mark', 'Melissa', 'Rick']
>>> result = [x for x in data]
>>>
>>> print(x)
Traceback (most recent call last):
NameError: name 'x' is not defined
12.1.6. Rationale
Manipulate Numbers
Manipulate Strings
Manipulate Numbers:
>>> data = [1, 2, 3, 4]
>>>
>>> [x**2 for x in data]
[1, 4, 9, 16]
Manipulate Strings:
>>> data = ['a', 'b', 'c', 'd']
>>>
>>> [x.upper() for x in data]
['A', 'B', 'C', 'D']
12.1.7. Performance
Date: 2024-12-05
Python: 3.13.0
IPython: 8.30.0
System: macOS 15.1.1
Computer: MacBook M3 Max
CPU: 16 cores (12 performance and 4 efficiency) / 3nm
RAM: 128 GB RAM LPDDR5
Case Study A - range(0,5)
:
>>> data = list(range(0,5))
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = []
... for x in data:
... result.append(x)
...
87.5 ns ± 19.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
89.4 ns ± 20.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
88.9 ns ± 19.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
89.4 ns ± 21 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
84.5 ns ± 16.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = [x for x in data]
...
84 ns ± 19.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
80.7 ns ± 16.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
83.9 ns ± 20.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
85.2 ns ± 20.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
82.7 ns ± 19.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
Case Study B - range(0,500)
:
>>> data = list(range(0,500))
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = []
... for x in data:
... result.append(x)
...
4.19 μs ± 194 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
4.17 μs ± 194 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
4.19 μs ± 220 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
4.2 μs ± 245 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
4.16 μs ± 240 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = [x for x in data]
...
2.89 μs ± 196 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
2.93 μs ± 193 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
2.93 μs ± 204 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
2.96 μs ± 158 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
2.9 μs ± 207 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
12.1.8. Use Case - 1
Increment
>>> DATA = [1, 2, 3, 4]
>>>
>>> [x+1 for x in DATA]
[2, 3, 4, 5]
12.1.9. Use Case - 2
Decrement
>>> DATA = [1, 2, 3, 4]
>>>
>>> [x-1 for x in DATA]
[0, 1, 2, 3]
12.1.10. Use Case - 3
Even or Odd
>>> DATA = [1, 2, 3, 4]
>>>
>>> [x%2==0 for x in DATA]
[False, True, False, True]
12.1.11. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Comprehension About Range
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result: list[int]` with
# numbers from 0 to 5 (without 5)
# 2. Non-functional requirements:
# - Use list comprehension
# - Use `range()` function
# - Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[int]` z
# liczbami od 0 do 5 (bez 5)
# 2. Non-functional requirements:
# - Użyj list comprehension
# - Użyj funkcji `range()`
# - Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `[... for ... in ...]`
# - `range()`
# - `range` parameter `start` is inclusive
# - `range` parameter `stop` is exclusive
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert type(result) is list, \
'Result should be a list'
>>> assert all(type(x) is int for x in result), \
'Result should be a list of int'
>>> result
[0, 1, 2, 3, 4]
"""
# Define `result: list[int]` with
# numbers from 0 to 5 (without 5)
# type: list[int]
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Comprehension About Squares
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result: list[int]` with
# numbers from 0 to 5 (without 5) raised to the power of two (squared)
# 2. Non-functional requirements:
# - Use list comprehension
# - Use `range()` function
# - Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[int]` z
# liczbami od 0 do 5 (bez 5) podniesionymi do potęgi dwa (kwadraty)
# 2. Non-functional requirements:
# - Użyj list comprehension
# - Użyj funkcji `range()`
# - Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `[... for ... in ...]`
# - `range()`
# - `range()` parameter `start` is inclusive
# - `range()` parameter `stop` is exclusive
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert type(result) is list, \
'Result should be a list'
>>> assert all(type(x) is int for x in result), \
'Result should be a list of int'
>>> result
[0, 1, 4, 9, 16]
"""
# Define `result: list[int]` with
# numbers from 0 to 5 (without 5) raised to the power of two (squared)
# type: list[int]
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Comprehension About Upper
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result: list[str]` with
# capitalized letters from `DATA`
# 2. Non-functional requirements:
# - Use `DATA` variable
# - Use list comprehension
# - Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[str]` z
# literami z `DATA` zapisanymi wielkimi literami
# 2. Non-functional requirements:
# - Użyj zmiennej `DATA`
# - Use list comprehension
# - Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `str.upper()`
# - `[... for ... in ...]`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert type(result) is list, \
'Result should be a list'
>>> assert all(type(x) is str for x in result), \
'Result should be a list of str'
>>> result
['A', 'B', 'C', 'D']
"""
DATA = ['a', 'b', 'c', 'd']
# Define `result: list[str]` with
# capitalized letters from `DATA`
# type: list[str]
result = ...