4.8. Sequence Unpack Assignment

4.8.1. Rationale

  • Scalar assignment

  • Vector assignment

4.8.2. Syntax

Scalar assignment:

>>> a = 1

Vector assignment:

>>> a, b = 1, 2

Multi assignment:

>>> a = b = 1, 2

4.8.3. Scalar Assignment

>>> a = 1
>>>
>>> print(a)
1

4.8.4. Vector Assignment

>>> a, b = 1, 2
>>>
>>> print(a)
1
>>> print(b)
2
>>> a, b = 1, 2
>>> a, b, c = 1, 2, 3
>>> a, b, c, d = 1, 2, 3, 4
>>> a, b, c, d, e = 1, 2, 3, 4, 5

4.8.5. Right-Side Brackets

Scalar assignments:

>>> a = 1, 2
>>> a = (1, 2)
>>> a = [1, 2]
>>> a = {1, 2}

Vector assignments:

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

Rationale:

>>> a, b = 1, 2
>>> a, b = (1, 2)

4.8.6. Left-Side Brackets

>>> (a) = (1)
>>>
>>> print(a)
1
>>> (a, b) = 1, 2
>>> (a, b) = (1, 2)
>>> (a, b, c) = (1, 2, 3)
>>> (a, b, c) = (1, 2, 3)
>>> (a, b, c) = [1, 2, 3]
>>> [a, b, c] = [1, 2, 3]
>>> [a, b, c] = (1, 2, 3)

4.8.7. Errors

>>> a, b, c = 1, 2, 3, 4
Traceback (most recent call last):
ValueError: too many values to unpack (expected 3)
>>> a, b, c = 1, 2
Traceback (most recent call last):
ValueError: not enough values to unpack (expected 4, got 3)
>>> {a, b, c} = {1, 2, 3}
Traceback (most recent call last):
SyntaxError: can't assign to literal
>>> {a, b, c} = 1, 2, 3  
Traceback (most recent call last):
SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?

4.8.8. Unpacking

>>> data = [1,2,3]
>>> a, b, c = data
>>>
>>>
>>> print(a)
1
>>>
>>> print(b)
2
>>>
>>> print(c)
3
>>> line = 'Mark,Watney,44'
>>> firstname, lastname, age = line.split(',')
>>>
>>>
>>> print(firstname)
Mark
>>>
>>> print(lastname)
Watney
>>>
>>> print(age)
44

4.8.9. Nested

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

4.8.10. Skipping Values

  • _ is regular variable name, not a special Python syntax

  • _ by convention is used for data we don't want to access in future

>>> _ = 'Mark Watney'
>>> print(_)
Mark Watney
>>> line = 'Mark,Watney,1'
>>> firstname, lastname, _ = line.split(',')
>>>
>>> print(firstname)
Mark
>>> print(lastname)
Watney
>>> line = 'Mark,Watney,1,2,3'
>>> firstname, lastname, _, _, _ = line.split(',')
>>>
>>> print(firstname)
Mark
>>> print(lastname)
Watney

4.8.11. Use Case - 0x01

  • Skip

>>> a, b, _ = 1, 2, 3
>>> a, _, _ = 1, 2, 3
>>> a, _, c = 1, 2, 3
>>> _, b, _ = 1, 2, 3
>>> _, _, c = 1, 2, 3

4.8.12. Use Case - 0x02

  • Passwd

>>> line = 'twardowski:x:1001:1001:Jan Twardowski:/home/twardowski:/bin/bash'
>>> line = line.split(':')
>>>
>>> username = line[0]
>>> fullname = line[4]
>>>
>>> print(username)
twardowski
>>> print(fullname)
Jan Twardowski
>>> line = 'twardowski:x:1001:1001:Jan Twardowski:/home/twardowski:/bin/bash'
>>> username, _, _, _, fullname, _, _ = line.split(':')
>>>
>>> print(username)
twardowski
>>> print(fullname)
Jan Twardowski

4.8.13. Use Case - 0x03

  • Important

>>> _, important, _ = 1, 2, 3
>>>
>>> print(important)
2
>>> _, (important, _) = [1, (2, 3)]
>>>
>>> print(important)
2
>>> _, _, important = (True, [1, 2, 3, 4], 5)
>>> important
5
>>> _, _,  important = (True, [1, 2, 3, 4], (5, True))
>>> important
(5, True)
>>>
>>> _, _, (important, _) = (True, [1, 2, 3, 4], (5, True))
>>> important
5

Python understands this as:

>>> _ = (True, [1, 2, 3, 4], (5, True))
>>>
>>> a,b,c = (object, object, object)
>>> a,b,(c,d) = (object, object, (object,object))

4.8.14. Assignments

Code 4.20. Solution
"""
* Assignment: Sequence UnpackAssignment List
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Split input data
    2. Separate ip address and host name
    3. Run doctests - all must succeed

Polish:
    1. Podziel dane wejściowe
    2. Odseparuj adres ip i nazwę hosta
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert ip is not Ellipsis, \
    'Assign result to variable: `ip`'
    >>> assert host is not Ellipsis, \
    'Assign result to variable: `hosts`'
    >>> assert type(ip) is str, \
    'Variable `ip` has invalid type, should be str'
    >>> assert type(host) is str, \
    'Variable `hosts` has invalid type, should be str'

    >>> ip
    '10.13.37.1'

    >>> host
    'nasa.gov'
"""

DATA = ['10.13.37.1', 'nasa.gov']

# str: first string: 10.13.37.1
ip = ...

# list[str]: second string: nasa.gov
host = ...

Code 4.21. Solution
"""
* Assignment: Sequence UnpackAssignment Split
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Split input data
    2. Separate ip address and host name
    3. Run doctests - all must succeed

Polish:
    1. Podziel dane wejściowe
    2. Odseparuj adres ip i nazwę hosta
    3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `str.split()`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert ip is not Ellipsis, \
    'Assign result to variable: `ip`'
    >>> assert host is not Ellipsis, \
    'Assign result to variable: `hosts`'
    >>> assert type(ip) is str, \
    'Variable `ip` has invalid type, should be str'
    >>> assert type(host) is str, \
    'Variable `hosts` has invalid type, should be str'

    >>> ip
    '10.13.37.1'

    >>> host
    'nasa.gov'
"""

DATA = '10.13.37.1 nasa.gov'

# str: first string: 10.13.37.1
ip = ...

# list[str]: second string: nasa.gov
host = ...