6.4. Iterable Nested
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
6.4.1. What is an Object?
Basic types are objects
Iterable are objects too
Everything is an object
tuple.mro()
list.mro()
set.mro()
>>> tuple.mro()
[<class 'tuple'>, <class 'object'>]
>>> list.mro()
[<class 'list'>, <class 'object'>]
>>> set.mro()
[<class 'set'>, <class 'object'>]
6.4.2. List of Lists
Also known as multidimensional lists or matrix
Readability differs depending on whitespaces:
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> c = [[1,2,3], [4,5,6], [7,8,9]]
>>> d = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
>>> e = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]]
>>> f = [[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
>>> g = [[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]]
6.4.3. List of Tuples
Readability differs depending on whitespaces:
>>> data = [
... ('Mark', 'Watney'),
... ('Melisa', 'Lewis'),
... ('Rick', 'Martinez'),
... ]
>>> data = [
... ('Mark', 'Watney'),
... ('Melisa', 'Lewis'),
... ('Rick', 'Martinez')]
>>> data = [('Mark', 'Watney'),
... ('Melisa', 'Lewis'),
... ('Rick', 'Martinez')]
6.4.4. List of Dicts
>>> data = [
... {'firstname': 'Mark', 'lastname': 'Watney'},
... {'firstname': 'Melisa', 'lastname': 'Lewis'},
... {'firstname': 'Rick', 'lastname': 'Martinez'},
... ]
6.4.5. Many Types
Content could be even more complex data structure with nested items:
>>> data = {
... 'firstname': 'Mark',
... 'lastname': 'Watney',
... 'birthdate': '2000-01-02',
... 'age': 24,
... 'cities': ('Houston', 'Pasadena'),
... 'states': ['Texas', 'California'],
... 'groups': {'users', 'staff', 'admin'},
... 'friends': [
... {'firstname': 'Melissa', 'lastname': 'Lewis'},
... {'firstname': 'Rick', 'lastname': 'Martinez'},
... {'firstname': 'Alex', 'lastname': 'Vogel'},
... {'firstname': 'Beth', 'lastname': 'Johanssen'},
... {'firstname': 'Chris', 'lastname': 'Beck'},
... ],
... }
6.4.6. Length
>>> data = [
... ('Mark', 'Watney'),
... ('Melisa', 'Lewis'),
... ('Rick', 'Martinez'),
... ]
>>> len(data)
3
>>> len(data[0])
2
>>> len(data[0][1])
6
6.4.7. Append vs. Extend
>>> from pprint import pprint
>>> data = [1, 2, 3]
>>> data.extend([4, 5, 6])
>>>
>>> data
[1, 2, 3, 4, 5, 6]
>>> data = [1, 2, 3]
>>> data.append([4, 5, 6])
>>>
>>> data
[1, 2, 3, [4, 5, 6]]
Append elements using list.append()
:
>>> result = [
... ('Mark', 'Watney'),
... ('Melisa', 'Lewis'),
... ('Rick', 'Martinez'),
... ]
>>>
>>> data = ('Alex', 'Vogel')
>>> result.append(data)
>>>
>>> pprint(result, width=30)
[('Mark', 'Watney'),
('Melisa', 'Lewis'),
('Rick', 'Martinez'),
('Alex', 'Vogel')]
Append elements using list.extend()
:
>>> result = [
... ('Mark', 'Watney'),
... ('Melisa', 'Lewis'),
... ('Rick', 'Martinez'),
... ]
>>>
>>> data = ('Alex', 'Vogel')
>>> result.extend(data)
>>>
>>> pprint(result, width=30)
[('Mark', 'Watney'),
('Melisa', 'Lewis'),
('Rick', 'Martinez'),
'Alex',
'Vogel']
6.4.8. Use Case - 1
One dimensional (1D) structure - vector
>>> data = [1, 2, 3]
Is equivalent to:
>>> obj1 = 1
>>> obj2 = 2
>>> obj3 = 3
>>>
>>> data = [obj1, obj2, obj3]
6.4.9. Use Case - 2
Two dimensional (2D) structure - matrix
>>> data = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
Is equivalent to:
>>> obj1 = [1, 2, 3]
>>> obj2 = [4, 5, 6]
>>> obj3 = [7, 8, 9]
>>>
>>> data = [obj1, obj2, obj3]
6.4.10. 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: Iterable Nested Definition
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3
# %% English
# 1. Create nested list `result` with elements:
# - tuple: 1, 2, 3
# - list: 1.1, 2.2, 3.3
# - set: 'red', 'green', 'blue'
# 2. Run doctests - all must succeed
# %% Polish
# 1. Stwórz zagnieżdżoną listę `result` z elementami:
# - tuple: 1, 2, 3
# - list: 1.1, 2.2, 3.3
# - set: 'red', 'green', 'blue'
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert len(result) == 3, \
'Variable `result` length should be 3'
>>> assert (1, 2, 3) in result
>>> assert [1.1, 2.2, 3.3] in result
>>> assert {'red', 'green', 'blue'} in result
"""
# Result should contain:
# - tuple: 1, 2, 3
# - list: 1.1, 2.2, 3.3
# - set: 'red', 'green', 'blue'
# type: list[tuple|list|set]
result = ...