4.4. Sequence Nesting

4.4.1. list of tuple

4.4.1.1. Getting elements

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[2]
# (7.6, 3.0, 6.6, 2.1, 'virginica')

DATA[2][1]
# 3.0

4.4.1.2. Appending elements

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)
# [(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')]
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)
# [(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']

4.4.1.3. 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[2])
# 5

4.4.1.4. Type Annotation

from typing import List


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'),
]
from typing import List, Tuple


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'),
]

4.4.2. list of list

  • Multidimensional lists

DATA = [[1,2,3],[4,5,6],[7,8,9]]
DATA = [[1,2,3], [4,5,6], [7,8,9]]
DATA = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
DATA = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

4.4.2.1. Getting elements

DATA = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

DATA[0][0]
# 1

DATA[0][2]
# 3

DATA[2][1]
# 8

4.4.2.2. Length

DATA = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

len(DATA)
# 3

len(DATA[2])
# 3

4.4.2.3. Type Annotations

from typing import List

DATA: List[list] = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]
from typing import List

DATA: List[List[int]] = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

4.4.3. Mixed types

4.4.3.1. Getting elements

DATA = [
    [1, 2, 3],
    (4, 5, 6),
    {7, 8, 9},
]

DATA[1]
# (4, 5, 6)

DATA[1][2]
# 6

DATA[2]
# {7, 8, 9}

4.4.3.2. 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

4.4.3.3. Type Annotations

from typing import Set, List, Union, Tuple


DATA: List[Union[List[int], Tuple[int, int, int], Set[int]]] = [
    [1, 2, 3],
    (4, 5, 6),
    {7, 8, 9},
]
from typing import List


DATA: List[Union[list, tuple, set]] = [
    [1, 2, 3],
    (4, 5, 6),
    {7, 8, 9},
]

4.4.4. Assignments

Todo

Create assignments