5. Nested Collections

5.1. list of tuple

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

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

element = (4.9, 2.5, 4.5, 1.7, 'virginica')
DATA.append(element)
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.append((4.9, 3.0, 1.4, 0.2, 'setosa'))

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

5.2. list of dict

5.2.1. Getting elements

DATA = [
    {'measurements': [4.7, 3.2, 1.3, 0.2], 'species': 'setosa'},
    {'measurements': [7.0, 3.2, 4.7, 1.4], 'species': 'versicolor'},
    {'measurements': [7.6, 3.0, 6.6, 2.1], 'species': 'virginica'},
]

DATA[0]
# {'measurements': [4.7, 3.2, 1.3, 0.2], 'species': 'setosa')

DATA[0]['measurements']
# [4.7, 3.2, 1.3, 0.2]

DATA[0]['species']
# 'setosa'
DATA = [
    {'measurements': [4.7, 3.2, 1.3, 0.2], 'species': 'setosa'},
    {'measurements': [7.0, 3.2, 4.7, 1.4], 'species': 'versicolor'},
    {'measurements': [7.6, 3.0, 6.6, 2.1], 'species': 'virginica'},
]

DATA[0].get('kind')
# KeyError: 'kind'

DATA[0].get('kind', 'n/a')
# 'n/a'

DATA[2].get('measurements')
# [7.6, 3.0, 6.6, 2.1]

DATA[2].get('measurements')[1]
# 3.0

5.2.2. Length

DATA = [
    {'measurements': [4.7, 3.2, 1.3, 0.2], 'species': 'setosa'},
    {'measurements': [7.0, 3.2, 4.7, 1.4], 'species': 'versicolor'},
    {'measurements': [7.6, 3.0, 6.6, 2.1], 'species': 'virginica'},
]

len(DATA)
# 3

len(DATA[0])
# 2

len(DATA[1])
# 2

len(DATA[1]['species'])
# 10

len(DATA[1]['measurements'])
# 4

5.3. 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],
]

5.3.1. Getting elements

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

array[0][0]
# 1

array[0][2]
# 3

array[2][1]
# 8

5.3.2. Length

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

len(DATA)
# 3

len(DATA[2])
# 3

5.4. Mixed types

5.4.1. Getting elements

DATA = [
    [1, 2, 3],
    (4, 5, 6),
    {7, 8, 9},
    {'species': 'virginica', 'measurements': [7.6, 3.0, 6.6, 2.1]}
]

DATA[1][2]
# 6

DATA[3]['species']
# 'virginica'

DATA[3].get('species')
# 'virginica'

5.4.2. Length

DATA = [
    [1, 2, 3],
    (4, 5, 6),
    {7, 8, 9},
    {'species': 'virginica', 'measurements': [7.6, 3.0, 6.6, 2.1]}
]

len(DATA)
# 4

len(DATA[0])
# 3

len(DATA[3])
# 2

len(DATA[3]['measurements'])
# 4

5.5. Assignments

5.5.1. Select

  • Complexity level: easy

  • Lines of code to write: 6 lines

  • Estimated time of completion: 15 min

  • Filename: solution/nested_select.py

English
  1. For given data input (see below)

  2. Write header (first line) to header variable

  3. Create list output

  4. Convert to list data from row 2, 6, 9 and add to output

  5. Convert to tuple data from row 12, 15, 16 and add to output

  6. Convert to dict data from row 18, 21 and add to output:

    • key -> index number (18 or 21)

    • value -> species name

  7. Add empty set to output

  8. Use only indexes

  9. Do not use for, while or slice()

Polish
  1. Dla danych wejściowych (patrz poniżej)

  2. Zapisz nagłówek (pierwsza linia) do zmiennej header

  3. Stwórz listę output

  4. Przekonwertuj do list dane z wierszy 2, 6, 9 i dodaj do output

  5. Przekonwertuj do tuple dane z wierszy 12, 15, 16 i dodaj do output

  6. Przekonwertuj do dict dane z wierszy 18, 21 i dodaj do output:

    • klucz -> numer indeksu (18 or 21)

    • wartość -> nazwa gatunku

  1. Dodaj pusty set do output

  2. Użyj tylko indeksów

  3. Nie używaj for, while lub slice()

Input
INPUT = [
    ('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'),
    (7.0, 3.2, 4.7, 1.4, 'versicolor'),
    (7.6, 3.0, 6.6, 2.1, 'virginica'),
    (4.9, 3.0, 1.4, 0.2, 'setosa'),
    (4.9, 2.5, 4.5, 1.7, 'virginica'),
    (7.1, 3.0, 5.9, 2.1, 'virginica'),
    (4.6, 3.4, 1.4, 0.3, 'setosa'),
    (5.4, 3.9, 1.7, 0.4, 'setosa'),
    (5.7, 2.8, 4.5, 1.3, 'versicolor'),
    (5.0, 3.6, 1.4, 0.3, 'setosa'),
    (5.5, 2.3, 4.0, 1.3, 'versicolor'),
    (6.5, 3.0, 5.8, 2.2, 'virginica'),
    (6.5, 2.8, 4.6, 1.5, 'versicolor'),
    (6.3, 3.3, 6.0, 2.5, 'virginica'),
    (6.9, 3.1, 4.9, 1.5, 'versicolor'),
    (4.6, 3.1, 1.5, 0.2, 'setosa'),
]
The whys and wherefores
  • Using nested data structures

  • Using indexes

  • Type casting