4.8. Unpack Assignment

  • a = 1 - Assignment

  • a, b = 1, 2 - Unpacking assignment

  • a = b = 1 - Chained assignment

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

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

Assignment:

>>> a = 1

Unpacking assignment:

>>> a, b = 1, 2

Chained assignment:

>>> a = b = 1

4.8.1. Assignment

  • identifier = value

  • a = 1

  • Scalar Assignment

>>> a = 1
>>>
>>> print(f'{a=}')
a=1

4.8.2. Unpacking Assignment

  • sequence[identifier] = sequence[value]

  • a, b = 1, 2

  • Vector Assignment

  • Sequence Assignment

>>> a, b = 1, 2
>>>
>>> print(f'{a=}, {b=}')
a=1, 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.3. Chained Assignment

  • a = b = 1

  • identifier1 = identifier2 = value

>>> a = b = 1
>>>
>>> print(f'{a=}, {b=}')
a=1, b=1
>>> a = b = 1
>>> a = b = c = 1
>>> a = b = c = d = 1
>>> a = b = c = d = e = 1

4.8.4. Right-Side Brackets

Scalar assignments:

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

Unpacking 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.5. 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.6. 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 3, got 2)
>>> {a, b, c} = {1, 2, 3}
Traceback (most recent call last):
SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
>>> {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.7. Unpacking

>>> data = [1, 2, 3]
>>> a, b, c = data
>>>
>>> print(f'{a=}, {b=}, {c=}')
a=1, b=2, c=3
>>> line = 'Mark,Watney,40'
>>> firstname, lastname, age = line.split(',')
>>>
>>> print(f'{firstname=}, {lastname=}, {age=}')
firstname='Mark', lastname='Watney', age='40'
>>> data = ['Mark', 'Watney', ('mwatney@nasa.gov', 'mwatney@gmail.com')]
>>> firstname, lastname, emails = data
>>>
>>> print(f'{firstname=}\n{lastname=}\n{emails=}')
firstname='Mark'
lastname='Watney'
emails=('mwatney@nasa.gov', 'mwatney@gmail.com')

4.8.8. Nested

>>> a, (b, c) = [1, (2, 3)]
>>>
>>> print(f'{a=}, {b=}, {c=}')
a=1, b=2, c=3
>>> data = ['Mark', 'Watney', ('mwatney@nasa.gov', 'mwatney@gmail.com')]
>>> firstname, lastname, (email_work, email_home) = data
>>>
>>> print(f'{firstname=}\n{lastname=}\n{email_work=}\n{email_home=}')
firstname='Mark'
lastname='Watney'
email_work='mwatney@nasa.gov'
email_home='mwatney@gmail.com'

4.8.9. 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,40'
>>> firstname, lastname, _ = line.split(',')
>>>
>>> print(f'{firstname=}, {lastname=}')
firstname='Mark', lastname='Watney'
>>> line = 'Mark,Watney,40,185,75.5'
>>> firstname, lastname, _, _, _ = line.split(',')
>>>
>>> print(f'{firstname=}, {lastname=}')
firstname='Mark', lastname='Watney'

4.8.10. Use Case - 0x01

  • Skip

>>> a, b, _ = 'red', 'green', 'blue'
>>> a, _, _ = 'red', 'green', 'blue'
>>> a, _, c = 'red', 'green', 'blue'
>>> _, b, _ = 'red', 'green', 'blue'
>>> _, _, c = 'red', 'green', 'blue'

4.8.11. Use Case - 0x02

  • Passwd

>>> line = 'watney:x:1000:1000:Mark Watney:/home/watney:/bin/bash'
>>> username, _, uid, _, _, _, _ = line.split(':')
>>>
>>> print(f'{username=}, {uid=}')
username='watney', uid='1000'

4.8.12. 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)
>>>
>>> print(important)
5
>>> _, _,  important = (True, [1, 2, 3, 4], (5, True))
>>>
>>> print(important)
(5, True)
>>>
>>> _, _, (important, _) = (True, [1, 2, 3, 4], (5, True))
>>>
>>> print(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.13. Recap

  • Scalar, unpacking assignment, chained assignment

  • Both left and right expression side brackets are optional

  • Unpacking nested sequences

  • Skipping values is done by using _

Scalar assignment:

>>> a = 1

Unpacking assignment:

>>> a, b = 1, 2

Chained assignment:

>>> a = b = 1

Unpacking nested:

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

Skipping:

>>> _, (important, _) = 1, (2, 3)

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. Separate ip address and host name
    2. Run doctests - all must succeed

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

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

    >>> assert ip is not Ellipsis, \
    'Assign your result to variable `ip`'
    >>> assert host is not Ellipsis, \
    'Assign your result to variable `host`'
    >>> 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']

# String with IP address: 10.13.37.1
# type: str
ip = ...

# String with host name: nasa.gov
# type: list[str]
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 your result to variable `ip`'
    >>> assert host is not Ellipsis, \
    'Assign your result to variable `host`'
    >>> 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'

# String with IP address: 10.13.37.1
# type: str
ip = ...

# String with host name: nasa.gov
# type: list[str]
host = ...