9.2. Nested List of Tuples

  • Iterable is an object

  • Iterable element is an object too

  • Therefore an element of a Iterable could be another Iterable

  • There is no limit how nested it could be

Examples:

>>> users = [
...     ('Alice', 'Apricot'),
...     ('Bob', 'Blackthorn'),
...     ('Carol', 'Corn'),
... ]

9.2.1. Format

  • Readability differs depending on whitespaces

>>> data = [
...     ('Alice', 'Apricot'),
...     ('Bob', 'Blackthorn'),
...     ('Carol', 'Corn'),
... ]
>>> data = [
...     ('Alice', 'Apricot'),
...     ('Bob', 'Blackthorn'),
...     ('Carol', 'Corn')]
>>> data = [('Alice', 'Apricot'),
...         ('Bob', 'Blackthorn'),
...         ('Carol', 'Corn')]

9.2.2. List-of-Pairs to/from Dict

Convert list of pairs to dict:

>>> data = [
...     ('firstname', 'Alice'),
...     ('lastname', 'Apricot'),
...     ('age', 30),
... ]
>>>
>>> dict(data)
{'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30}

Convert dict to list of pairs:

>>> data = {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30}
>>>
>>> list(data.items())
[('firstname', 'Alice'),
 ('lastname', 'Apricot'),
 ('age', 30)]

9.2.3. Length

>>> data = [
...     ('Alice', 'Apricot'),
...     ('Bob', 'Blackthorn'),
...     ('Carol', 'Corn'),
... ]
>>> len(data)
3
>>> len(data[0])
2
>>> len(data[0][0])
5

9.2.4. Assignments

# %% About
# - Name: Nested ListTuple Dict
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% 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

# %% English
# 1. Use `DATA: list[tuple]`
# 2. Define `result: dict` with `DATA` converted to `dict`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Użyj `DATA: list[tuple]`
# 2. Zdefiniuj `result: dict` z przekonwertowanym `DATA` do `dict`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# {'firstname': 'Alice',
#  'lastname': 'Apricot',
#  'age': 30}

# %% Hints
# - `dict()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from pprint import pprint

>>> assert type(result) is dict, \
'Variable `result` has an invalid type; expected: `dict`.'

>>> assert all(type(x) is str for x in result.keys()), \
'Variable `result.keys()` has elements of an invalid type; all items should be: `str`.'

>>> assert 'firstname' in result.keys()
>>> assert 'lastname' in result.keys()
>>> assert 'age' in result.keys()

>>> assert 'Alice' in result.values()
>>> assert 'Apricot' in result.values()
>>> assert 30 in result.values()

>>> pprint(result, width=40, sort_dicts=False)
{'firstname': 'Alice',
 'lastname': 'Apricot',
 'age': 30}
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
result: dict[str, str | int]

# %% Data
DATA = [
    ('firstname', 'Alice'),
    ('lastname', 'Apricot'),
    ('age', 30),
]

# %% Result
result = ...