7.3. Loop Nested Sequences

7.3.1. Nested sequence of objects of one type

Listing 102. Iterating over sequence
DATA = [1, 2, 3]

for obj in DATA:
    print(obj)

# 1
# 2
# 3
Listing 103. Iterating over nested sequence
DATA = [(...), (...), (...)]

for obj in DATA:
    print(obj)

# (...)
# (...)
# (...)
Listing 104. Iterating over nested sequence
DATA = [
    ('a', 1),
    ('b', 2),
    ('c', 3),
]

for obj in DATA:
    print(obj)

# ('a', 1)
# ('b', 2)
# ('c', 3)
Listing 105. Iterating over nested sequence
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 row in DATA:
    print(row)

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

7.3.2. Nested sequence of objects of many types

Listing 106. Iterating over list with scalar and vector values - simple loop
DATA = [('Jan', 'Twardowski'), 'Watney', 42, 13.37, {True, None, False}]

for obj in DATA:
    print(obj)

# ('Jan', 'Twardowski')
# Watney
# 42
# 13.37
# {False, True, None}
Listing 107. Iterating over list with scalar and vector values - nested loop
DATA = [('Jan', 'Twardowski'), 'Watney', 42, 13.37, {True, None, False}]

for obj in DATA:
    for inner in obj:
        print(inner)

# Jan
# Twardowski
# W
# a
# t
# n
# e
# y
# Traceback (most recent call last):
#   File "<input>", line 4, in <module>
# TypeError: 'int' object is not iterable
Listing 108. Iterating over list with scalar and vector values - smart loop
DATA = [('Jan', 'Twardowski'), 'Watney', 42, 13.37, {True, None, False}]


for obj in DATA:
    if isinstance(obj, (list, set, tuple)):
        for inner in obj:
            print(inner)
    else:
        print(obj)

# Jan
# Twardowski
# Watney
# 42
# 13.37
# False
# True
# None

7.3.3. Assignments

7.3.3.1. Mean

  • Complexity level: easy

  • Lines of code to write: 3 lines

  • Estimated time of completion: 10 min

  • Solution: solution/nested_mean.py

English
  1. For input data (see below)

  2. Separate header from data

  3. Calculate mean Sepal length value

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

  2. Oddziel nagłówek od danych

  3. Wylicz średnią wartość Sepal length

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
  • Iterating over nested structure

7.3.3.2. Unique keys from schema-less database

English
  1. For input data (see below)

  2. Collect keys from all rows in one sequence output

  3. Sort output

  4. Print unique keys

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

  2. Zbierz klucze z wszystkich wierszy w jednej sekwencji output

  3. Posortuj output

  4. Wypisz unikalne klucze

Input
INPUT = [
    {'Sepal length': 5.1, 'Sepal width': 3.5, 'Species': 'setosa'},
    {'Petal length': 4.1, 'Petal width': 1.3, 'Species': 'versicolor'},
    {'Sepal length': 6.3, 'Petal width': 1.8, 'Species': 'virginica'},
    {'Sepal length': 5.0, 'Petal width': 0.2, 'Species': 'setosa'},
    {'Sepal width': 2.8, 'Petal length': 4.1, 'Species': 'versicolor'},
    {'Sepal width': 2.9, 'Petal width': 1.8, 'Species': 'virginica'},
]
Output
Petal length
Petal width
Sepal length
Sepal width
Species
The whys and wherefores
  • Generating set

  • Deduplication

  • Accessing dict keys

  • Iterating over nested structure

  • Updating set

Hint