3.5. Sequence Nested

3.5.1. Rationale

>>> obj = 1
>>> data = [obj, obj, obj]
>>> data
[1, 1, 1]
>>> obj = [1, 2, 3]
>>> data = [obj, obj, obj]
>>> data  # doctest: +NORMALIZE_WHITESPACE
[[1, 2, 3],
 [1, 2, 3],
 [1, 2, 3]]

3.5.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]]

Length:

>>> data = [[1, 2, 3],
...         [4, 5, 6],
...         [7, 8, 9]]
>>>
>>> len(data)
3
>>> len(data[0])
3

3.5.3. List of Tuples

Readability differs depending on whitespaces:

>>> data = [(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 = [
...     (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 = [
...     (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'),
... ]

Append elements using list.append():

>>> data = [(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')]
>>>
>>> row = (4.9, 2.5, 4.5, 1.7, 'virginica')
>>>
>>> data.append(row)
>>> data  # doctest: +NORMALIZE_WHITESPACE
[(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'),
 (4.9, 2.5, 4.5, 1.7, 'virginica')]

Append elements using list.extend():

>>> data = [(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')]
>>>
>>> row = (4.9, 2.5, 4.5, 1.7, 'virginica')
>>>
>>> data.extend(row)
>>> data  # doctest: +NORMALIZE_WHITESPACE
[(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'),
 4.9,
 2.5,
 4.5,
 1.7,
 'virginica']

Length:

>>> data = [(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')]
>>>
>>> len(data)
3
>>> len(data[0])
5

3.5.4. Many Types

Readability differs depending on whitespaces:

>>> data = [
...     [1, 2],
...     (3, 4, 5, 6),
...     {7, 8, 9, 10, 11}]
>>>
>>> data = [
...     [1, 2],
...     (3, 4, 5, 6),
...     {7, 8, 9, 10, 11}
... ]
>>>
>>> data = [[1, 2],
...         (3, 4, 5, 6),
...         {7, 8, 9, 10, 11}]

Length:

>>> data = [[1, 2],
...         (3, 4, 5, 6),
...         {7, 8, 9, 10, 11}]
>>>
>>> len(data)
3
>>> len(data[0])
2
>>> len(data[1])
4
>>> len(data[2])
5

3.5.5. Assignments

Code 3.12. Solution
"""
* Assignment: Sequence Nested Create
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
    1. Create nested sequence `result` with elements:
        a. tuple: `1`, `2`, `3`
        b. list: `1.1`, `2.2`, `3.3`
        c. set: `'Mark Watney'`, `'Melissa Lewis'`, `'Jan Twardowski'`
    2. Print `result`
    3. Print number of elements in `result`

Polish:
    1. Stwórz zagnieżdżoną sekwencję `result` z elementami:
        a. tuple: `1`, `2`, `3`
        b. list: `1.1`, `2.2`, `3.3`
        c. set: `'Mark Watney'`, `'Melissa Lewis'`, `'Jan Twardowski'`
    2. Wypisz `result`
    3. Wypisz liczbę elementów `result`

Tests:
    >>> import sys
    >>> sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assignment solution must be in `result` instead of ... (Ellipsis)'

    >>> type(result)
    <class 'list'>
    >>> len(result)
    3

    >>> (1, 2, 3) in result
    True
    >>> [1.1, 2.2, 3.3] in result
    True
    >>> {'Mark Watney', 'Melissa Lewis', 'Jan Twardowski'} in result
    True
"""

# Given
result = ...  # list with tuple 1, 2, 3 and list 1.1, 2.2, 3.3 and set 'Mark Watney', 'Melissa Lewis', 'Jan Twardowski'