7.4. Loop Unpacking Sequences

7.4.1. Unpacking values in loop

7.4.1.1. Recap information about unpacking

Listing 104. Unpacking values
a, b = 1, 2
a, b = (1, 2)
k, v = (1, 2)
key, value = (1, 2)

7.4.1.2. Unpacking list of pairs

Listing 105. Unpacking values in loop
DATA = [
    ('a', 1),
    ('b', 2),
    ('c', 3),
]

for key, value in DATA:
    print(f'{key} -> {value}')

# a -> 1
# b -> 2
# c -> 3
Listing 106. Unpacking values in loop
DATA = [
    (1, 2),
    ('name', 'Jan Twardowski'),
    ('species', ['setosa', 'versicolor', 'virginica']),
    ((1, 2), ['Johnson Space Center', 'Kennedy Space Center']),
]

for key, value in DATA:
    print(f'{key} -> {value}')

# 1 -> 2
# name -> Jan Twardowski
# species -> ['setosa', 'versicolor', 'virginica']
# (1, 2) -> ['Johnson Space Center', 'Kennedy Space Center']

7.4.1.3. Unpacking list of sequences

Listing 107. Unpacking values in loop
DATA = [
    (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'),
]

# sepal_len, sepal_wid, petal_len, petal_wid, species = (5.1, 3.5, 1.4, 0.2, 'setosa')

for sepal_len, sepal_wid, petal_len, petal_wid, species in DATA:
    print(f'{species} -> {sepal_len}')

# setosa -> 5.1
# versicolor -> 5.7
# virginica -> 6.3

7.4.1.4. list of dict

Listing 108. list of dict
DATA = [
    {'Sepal length': 5.1, 'Sepal width': 3.5, 'Petal length': 1.4, 'Petal width': 0.2, 'Species': 'setosa'},
    {'Sepal length': 5.7, 'Sepal width': 2.8, 'Petal length': 4.1, 'Petal width': 1.3, 'Species': 'versicolor'},
    {'Sepal length': 6.3, 'Sepal width': 2.9, 'Petal length': 5.6, 'Petal width': 1.8, 'Species': 'virginica'},
]

for row in DATA:
    sepal_length = row['Sepal length']
    species = row['Species']
    print(f'{species} -> {sepal_length}')

# setosa -> 5.1
# versicolor -> 5.7
# virginica -> 6.3

7.4.2. Enumerating and item index

Listing 109. Enumerating and item index
DATA = [
    (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'),
]

for i, row in enumerate(DATA):
    print(f'{i} -> {row}')

# 0 -> (5.1, 3.5, 1.4, 0.2, 'setosa')
# 1 -> (5.7, 2.8, 4.1, 1.3, 'versicolor')
# 2 -> (6.3, 2.9, 5.6, 1.8, 'virginica')

7.4.3. Assignments

7.4.3.1. Unpacking

English
  1. For input data (see below)

  2. Separate header from data

  3. Iterating over data unpack row to *features and label

  4. Print species names ending with "ca" or "osa"

Polish
  1. Dla danych wejściowych (patrz sekcja input)

  2. Oddziel nagłówek od danych

  3. Iterując po danych rozpakuj wiersz do *features oraz label

  4. Wypisz nazwy gatunków kończące się na "ca" lub "osa"

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.6, 3.1, 1.5, 0.2, {'setosa'}),
]
The whys and wherefores
  • Accessing set items

  • Iterating over nested structure

  • Unpacking in for loop