4. Unpacking sequences

4.1. Unpacking values

a, b, c = 1, 2, 3
a, b, c = (1, 2, 3)
a, b, c = [1, 2, 3]
a, b, c = {1, 2, 3}

Note

Note, that set is unordered collection!

4.1.1. Too many values to unpack

a, b, c = [1, 2, 3, 4]
# ValueError: too many values to unpack (expected 3)

4.1.2. Not enough values to unpack

a, b, c, d = [1, 2, 3]
# ValueError: not enough values to unpack (expected 4, got 3)

4.2. Unpacking arbitrary number of arguments

4.2.1. Unpacking values at the right side

a, b, *others = [1, 2, 3, 4]

a           # 1
b           # 2
others      # [3, 4]

4.2.2. Unpacking values at the left side

*others, a, b = [1, 2, 3, 4]

others      # [1, 2]
a           # 3
b           # 4

4.2.3. Unpacking values at the left side

first, *middle, last = [1, 2, 3, 4]

first       # 1
middle      # [2, 3]
last        # 4

4.2.4. Cannot unpack from both sides at once

*a, b, *c = [1, 2, 3, 4]
# SyntaxError: two starred expressions in assignment

4.3. Unpacking values from function

4.3.1. Recap of assignment information

line = '4.9,3.1,1.5,0.1,setosa'

line.split(',')
# ['4.9', '3.1', '1.5', '0.1', 'setosa']
line = '4.9,3.1,1.5,0.1,setosa'

sepal_length, sepal_width, petal_length, petal_width, species = line.split(',')

sepal_length    # '4.9'
sepal_width     # '3.1'
petal_length    # '1.5'
petal_width     # '0.1'
species         # 'setosa'

4.3.2. Unpacking values at the right side

line = '4.9,3.1,1.5,0.1,setosa'

sepal_length, sepal_width, *others = line.split(',')

sepal_length    # '4.9'
sepal_width     # '3.1'
others          # ['1.5', '0.1', 'setosa']

4.3.3. Unpacking values at the left side

line = '4.9,3.1,1.5,0.1,setosa'

*features, species = line.split(',')

features        # ['4.9', '3.1', '1.5', '0.1']
species         # 'setosa'

4.3.4. Cannot unpack from both sides at once

line = '4.9,3.1,1.5,0.1,setosa'

*a, b, *c = line.split(',')
# SyntaxError: two starred expressions in assignment

4.3.5. Naming convention

  • if you’re not using features later in your code

line = '4.9,3.1,1.5,0.1,setosa'

*_, species = line.split(',')

4.4. Example

def get_iris():
    """
    Would be nice, if you can get ``dict``...
    but most programmers will return ``tuple``
    because it's a bit faster

    return {
        'sepal_length': 4.9,
        'sepal_width': 3.1,
        'petal_length': 1.5,
        'petal_width': 0.1,
        'species': 'setosa'
    }
    """
    return 4.9, 3.1, 1.5, 0.1, 'setosa'

*features, species = get_iris()

4.5. More advanced topics

Note

The topic will be continued in Intermediate and Advanced part of the book

4.6. Assignments

4.6.1. Hosts

Todo

Poprawić zadanie. Wykorzystuje rzeczy, których nie było wprowadzonych

  • Filename: kwargs_hosts.py

  • Lines of code to write: 15 lines

  • Estimated time of completion: 15 min

  • Input data: Code Listing 4.1.

  1. Skopiuj zawartość listingu poniżej do pliku hosts.txt

  2. Stwórz pusty dict o nazwie hosts

  3. Czytając plik pomiń puste linie lub zaczynające się od komentarza #

  4. Do hosts dla klucza IP dodaj listę hostname

  5. Przy parsowaniu linii skorzystaj z konstrukcji z gwiazdką *

Code Listing 4.1. Listing pliku /etc/hosts
##
# ``/etc/hosts`` structure:
#   - IPv4 or IPv6
#   - Hostnames
##

127.0.0.1       localhost
127.0.0.1       astromatt
10.13.37.1      nasa.gov esa.int roscosmos.ru
255.255.255.255 broadcasthost
::1             localhost