3.6. Typing Nested
3.6.1. List of Lists
>>> data: list = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
>>> data: list[list] = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
>>> data: list[list[int]] = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
3.6.2. List of Tuples
>>> data: list = [
... (4.7, 3.2, 1.3, 0.2, 'setosa'),
... (7.0, 3.2, 4.7, 1.4, 'versicolor'),
... (7.6, 3.0, 6.6, 2.1, 'virginica'),
... ]
>>> data: list[tuple] = [
... (4.7, 3.2, 1.3, 0.2, 'setosa'),
... (7.0, 3.2, 4.7, 1.4, 'versicolor'),
... (7.6, 3.0, 6.6, 2.1, 'virginica'),
... ]
>>> data: list[tuple[float,float,float,float,str]] = [
... (4.7, 3.2, 1.3, 0.2, 'setosa'),
... (7.0, 3.2, 4.7, 1.4, 'versicolor'),
... (7.6, 3.0, 6.6, 2.1, 'virginica'),
... ]
3.6.3. List of Dicts
>>> data: list = [
... {'features': [4.7, 3.2, 1.3, 0.2], 'label': 'setosa'},
... {'features': [7.0, 3.2, 4.7, 1.4], 'label': 'versicolor'},
... {'features': [7.6, 3.0, 6.6, 2.1], 'label': 'virginica'},
... ]
>>> data: list[dict] = [
... {'features': [4.7, 3.2, 1.3, 0.2], 'label': 'setosa'},
... {'features': [7.0, 3.2, 4.7, 1.4], 'label': 'versicolor'},
... {'features': [7.6, 3.0, 6.6, 2.1], 'label': 'virginica'},
... ]
>>> data: list[dict[str, list[float]|str]] = [
... {'features': [4.7, 3.2, 1.3, 0.2], 'label': 'setosa'},
... {'features': [7.0, 3.2, 4.7, 1.4], 'label': 'versicolor'},
... {'features': [7.6, 3.0, 6.6, 2.1], 'label': 'virginica'},
... ]
3.6.4. Further Reading
More information in Type Annotations
More information in CI/CD Type Checking
3.6.5. References
3.6.6. 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: Typing Annotations List of Tuple
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Add type annotations to the variable
# 2. Run doctests - all must succeed
# %% Polish
# 1. Dodaj adnotacje typów do zmiennej
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> import importlib
>>> from typing import get_type_hints, get_origin
>>> module = importlib.import_module(__name__)
>>> annotations = get_type_hints(module)
>>> assert annotations['data'] == list[list[int]]
>>> assert get_origin(annotations['data']) == list
>>> assert data == [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ], \
'Do not modify variable `data` value, just add type annotation'
"""
# Add type annotations to the variable
data = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
# %% 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: Typing Annotations List of Tuple
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Add type annotations to the variable
# 2. Run doctests - all must succeed
# %% Polish
# 1. Dodaj adnotacje typów do zmiennej
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> import importlib
>>> from typing import get_type_hints, get_origin
>>> module = importlib.import_module(__name__)
>>> annotations = get_type_hints(module)
>>> assert get_origin(annotations['data']) == list
>>> assert data == [
... (25.91375, -60.15503),
... (-11.01983, -166.48477),
... (-11.01983, -166.48477),
... ], \
'Do not modify variable `data` value, just add type annotation'
"""
# Add type annotations to the variable
data = [
(25.91375, -60.15503),
(-11.01983, -166.48477),
(-11.01983, -166.48477),
]
# %% 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: Typing Annotations List of Dict
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Add type annotations to the variable
# 2. Run doctests - all must succeed
# %% Polish
# 1. Dodaj adnotacje typów do zmiennej
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> import importlib
>>> from typing import get_type_hints, get_origin
>>> module = importlib.import_module(__name__)
>>> annotations = get_type_hints(module)
>>> assert get_origin(annotations['data']) == list
>>> assert data == [
... {'features': [4.7, 3.2, 1.3, 0.2], 'label': 'setosa'},
... {'features': [7.0, 3.2, 4.7, 1.4], 'label': 'versicolor'},
... {'features': [7.6, 3.0, 6.6, 2.1], 'label': 'virginica'},
... ], \
'Do not modify variable `data` value, just add type annotation'
"""
# Add type annotations to the variable
data = [
{'features': [4.7, 3.2, 1.3, 0.2], 'label': 'setosa'},
{'features': [7.0, 3.2, 4.7, 1.4], 'label': 'versicolor'},
{'features': [7.6, 3.0, 6.6, 2.1], 'label': 'virginica'},
]
# %% 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: Typing Annotations Mapping
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Add type annotations to the variable
# 2. Run doctests - all must succeed
# %% Polish
# 1. Dodaj adnotacje typów do zmiennej
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> import importlib
>>> from typing import get_type_hints, get_origin
>>> module = importlib.import_module(__name__)
>>> annotations = get_type_hints(module)
>>> assert get_origin(annotations['data']) == list
>>> assert data == [
... (5.8, 2.7, 5.1, 1.9, 'virginica'),
... (5.1, 3.5, 1.4, 0.2, 'setosa'),
... (5.7, 2.8, 4.1, 1.3, 'versicolor'),
... (6.3, 2.9, 5.6, 1.8, 'virginica'),
... (6.4, 3.2, 4.5, 1.5, 'versicolor'),
... (4.7, 3.2, 1.3, 0.2, 'setosa'),
... ], \
'Do not modify variable `data` value, just add type annotation'
"""
# Add type annotations to the variable
data = [
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
]
# %% 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: Typing Annotations Mapping
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Add type annotations to the variable
# 2. Run doctests - all must succeed
# %% Polish
# 1. Dodaj adnotacje typów do zmiennej
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> import importlib
>>> from typing import get_type_hints, get_origin
>>> module = importlib.import_module(__name__)
>>> annotations = get_type_hints(module)
>>> assert get_origin(annotations['data']) == list
>>> assert data == [
... ('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
... (5.8, 2.7, 5.1, 1.9, 'virginica'),
... (5.1, 3.5, 1.4, 0.2, 'setosa'),
... (5.7, 2.8, 4.1, 1.3, 'versicolor'),
... (6.3, 2.9, 5.6, 1.8, 'virginica'),
... (6.4, 3.2, 4.5, 1.5, 'versicolor'),
... (4.7, 3.2, 1.3, 0.2, 'setosa'),
... ], \
'Do not modify variable `data` value, just add type annotation'
"""
# Add type annotations to the variable
data = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
]