9.3. 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:

>>> name = 'Alice'

Unpacking assignment:

>>> firstname, lastname = 'Alice', 'Apricot'

Chained assignment:

>>> name1 = name2 = 'Alice'

Chained unpacking assignment:

>>> firstname1, lastname1 = firstname2, lastname2 = 'Alice', 'Apricot'

9.3.1. Case Study

SetUp:

>>> def get_user():
...     return 'Alice', 'Apricot'

You can use getitem to extract values:

>>> user = get_user()
>>>
>>> firstname = user[0]
>>> lastname = user[1]

Or you can use unpack assignment

>>> firstname, lastname = get_user()

In both cases result is the same:

>>> firstname
'Alice'
>>>
>>> lastname
'Apricot'

9.3.2. Assignment

  • Scalar Assignment

  • identifier = object

  • name = 'Alice'

  • name = 'Alice', 'Apricot'

>>> name = 'Alice'
>>>
>>> name
'Alice'
>>> name = 'Alice', 'Apricot'
>>>
>>> name
('Alice', 'Apricot')

9.3.3. Unpacking Assignment

  • iterable[identifier] = iterable[object]

  • firstname, lastname = 'Alice', 'Apricot'

  • a, b, c = 1, 2, 3

  • Sequence -> tuple, list

  • Iterable -> tuple, list, set, frozenset, dict, ...

  • Length at right and left side must be the same

>>> firstname, lastname = 'Alice', 'Apricot'
>>>
>>>
>>> firstname
'Alice'
>>>
>>> lastname
'Apricot'

This also work if there are more identifiers and values:

>>> firstname, lastname, age, email = 'Alice', 'Apricot', 30, 'alice@example.com'
>>>
>>>
>>> firstname
'Alice'
>>>
>>> lastname
'Apricot'
>>>
>>> age
30
>>>
>>> email
'alice@example.com'

But, the number of both identifiers and values must be equal.

You will get an error if there is more identifiers than values:

>>> firstname, lastname, age = 'Alice', 'Apricot'
Traceback (most recent call last):
ValueError: not enough values to unpack (expected 3, got 2)

Also, you will get an error if you get more values than identifiers:

>>> firstname, lastname = 'Alice', 'Apricot', 30
Traceback (most recent call last):
ValueError: too many values to unpack (expected 2, got 3)

9.3.4. Chained Assignment

  • identifier1 = identifier2 = object

  • name1 = name2 = 'Alice'

  • name1 = name2 = name3 = name4 = 'Alice'

>>> name1 = name2 = 'Alice'
>>>
>>>
>>> name1
'Alice'
>>>
>>> name2
'Alice'
>>> name1 = name2 = name3 = name4 = 'Alice'
>>>
>>>
>>> name1
'Alice'
>>>
>>> name2
'Alice'
>>>
>>> name3
'Alice'
>>>
>>> name4
'Alice'

9.3.5. Chained Unpacking Assignment

  • iterable[identifier] = iterable[identifier] = iterable[object]

  • firstname1, lastname1 = firstname2, lastname2 = 'Alice', 'Apricot'

>>> firstname1, lastname1 = firstname2, lastname2 = 'Alice', 'Apricot'
>>>
>>>
>>> firstname1
'Alice'
>>>
>>> lastname1
'Apricot'
>>>
>>> firstname2
'Alice'
>>>
>>> lastname2
'Apricot'

9.3.6. Brackets

  • firstname, lastname = 'Alice', 'Apricot'

  • firstname, lastname = ('Alice', 'Apricot')

  • firstname, lastname = ['Alice', 'Apricot']

  • (firstname, lastname) = 'Alice', 'Apricot'

  • (firstname, lastname) = ('Alice', 'Apricot')

  • (firstname, lastname) = ['Alice', 'Apricot']

  • [firstname, lastname] = 'Alice', 'Apricot'

  • [firstname, lastname] = ('Alice', 'Apricot')

  • [firstname, lastname] = ['Alice', 'Apricot']

Brackets does not define tuple, commas do:

>>> firstname, lastname = 'Alice', 'Apricot'

Identifier brackets are optional:

>>> firstname, lastname = 'Alice', 'Apricot'
>>> firstname, lastname = ('Alice', 'Apricot')
>>> firstname, lastname = ['Alice', 'Apricot']

But they can be tuples:

>>> (firstname, lastname) = 'Alice', 'Apricot'
>>> (firstname, lastname) = ('Alice', 'Apricot')
>>> (firstname, lastname) = ['Alice', 'Apricot']

Or lists:

>>> [firstname, lastname] = 'Alice', 'Apricot'
>>> [firstname, lastname] = ('Alice', 'Apricot')
>>> [firstname, lastname] = ['Alice', 'Apricot']

9.3.7. Nested

  • firstname, lastname, (email_work, email_school) = 'Alice', 'Apricot', ('alice@example.com', 'alice@example.edu')

>>> firstname, lastname, (email_work, email_school) = 'Alice', 'Apricot', ('alice@example.com', 'alice@example.edu')
>>>
>>>
>>> firstname
'Alice'
>>>
>>> lastname
'Apricot'
>>>
>>> email_work
'alice@example.com'
>>>
>>> email_school
'alice@example.edu'

9.3.8. Swap

  • Swap two variables

  • a = 'Alice'

  • b = 'Bob'

  • a, b = b, a

Generic way:

>>> a = 'Alice'
>>> b = 'Bob'
>>>
>>> c = a
>>> a = b
>>> b = c
>>>
>>>
>>> a
'Bob'
>>>
>>> b
'Alice'

Pythonic way:

>>> a = 'Alice'
>>> b = 'Bob'
>>>
>>> a, b = b, a
>>>
>>>
>>> a
'Bob'
>>>
>>> b
'Alice'

9.3.9. Use Case - 1

  • Imagine, that we have a Dragon with 100 gold and a Hero with 50 gold

  • Hero kills Dragon and takes all Dragon's gold

  • At the end, Hero has all gold (150) and Dragon has 0

>>> dragon_gold = 100
>>> hero_gold = 0
>>>
>>> hero_gold, dragon_gold = dragon_gold, 0
>>>
>>> hero_gold
100
>>>
>>> dragon_gold
0

9.3.10. Use Case - 2

  • Imagine, that we have a Dragon with 100 gold and a Hero with 50 gold

  • Hero kills Dragon and takes all Dragon's gold

  • At the end, Hero has all gold (150) and Dragon has 0

>>> dragon_gold = 100
>>> hero_gold = 10
>>>
>>> hero_gold, dragon_gold = hero_gold+dragon_gold, 0
>>>
>>> hero_gold
110
>>> dragon_gold
0

9.3.11. Use Case - 3

>>> line = '5.1,3.5,1.4,0.2,setosa'
>>> sl, sw, pl, pw, species = line.split(',')
>>>
>>>
>>> sl
'5.1'
>>>
>>> sw
'3.5'
>>>
>>> pl
'1.4'
>>>
>>> pw
'0.2'
>>>
>>> species
'setosa'

9.3.12. Use Case - 4

>>> log = '1969-07-21, 02:56:15, WARNING, Neil Armstrong first words on the Moon'
>>> date, time, level, message = log.split(', ')
>>>
>>>
>>> date
'1969-07-21'
>>>
>>> time
'02:56:15'
>>>
>>> level
'WARNING'
>>>
>>> message
'Neil Armstrong first words on the Moon'

9.3.13. Use Case - 5

>>> row = (5.8, 2.7, 5.1, 1.9, 'virginica')
>>> sl = row[0]
>>> sw = row[1]
>>> pl = row[2]
>>> pw = row[3]
>>> species = row[4]
>>>
>>> print(f'{sl=}, {sw=}, {pl=}, {pw=}, {species=}')
sl=5.8, sw=2.7, pl=5.1, pw=1.9, species='virginica'
>>> sl, sw, pl, pw, species = row
>>>
>>> print(f'{sl=}, {sw=}, {pl=}, {pw=}, {species=}')
sl=5.8, sw=2.7, pl=5.1, pw=1.9, species='virginica'

9.3.14. Recap

  • Four types of assignments: Scalar, Unpacking, Chained, Chained Unpacking Assignment

  • For unpacking assignment, lengths at both sides must be the same

  • Both left and right expression side brackets are optional

  • Unpacking nested sequences

Assignment:

>>> a = 1

Unpacking assignment:

>>> a, b = 1, 2

Chained assignment:

>>> a = b = 1

Chained unpacking assignment:

>>> a, b = c, d = 1, 2

Unpacking nested:

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

9.3.15. Assignments

# %% About
# - Name: Unpack Assignment List
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Separate ip address and host name
# 2. Define `ip: str` with IP address, i.e.: '127.0.0.1'
# 3. Define `host: str` with hostname, i.e.: 'localhost'
# 4. Use unpack assignment
# 5. Run doctests - all must succeed

# %% Polish
# 1. Odseparuj adres IP i nazwę hosta
# 2. Zdefiniuj `ip: str` z adresem IP, np.: '127.0.0.1'
# 3. Zdefiniuj `host: str` z nazwą hosta, np.: 'localhost'
# 4. Użyj przypisania z rozpakowaniem (unpack assignment)
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> ip
# '127.0.0.1'
#
# >>> host
# 'localhost'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert ip is not Ellipsis, \
'Variable `ip` has an invalid value; assign result of your program to it.'

>>> assert host is not Ellipsis, \
'Variable `host` has an invalid value; assign result of your program to it.'

>>> assert type(ip) is str, \
'Variable `ip` has an invalid type; expected: `str`.'

>>> assert type(host) is str, \
'Variable `host` has an invalid type; expected: `str`.'

>>> result = open(__file__).read()
>>> assert 'ip, host' + ' = ' + 'DATA' in result, \
'Use unpack assignment'

>>> ip
'127.0.0.1'

>>> host
'localhost'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
ip: str
host: str

# %% Data
DATA = ['127.0.0.1', 'localhost']

# %% Result

# %% About
# - Name: Unpack Assignment Split
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Split input data and separate ip address and host name
# 2. Define `ip: str` with IP address, i.e.: '127.0.0.1'
# 3. Define `host: str` with hostname, i.e.: 'localhost'
# 4. Use unpack assignment
# 5. Run doctests - all must succeed

# %% Polish
# 1. Podziel dane wejściowe i odseparuj adres ip i nazwę hosta
# 2. Zdefiniuj `ip: str` z adresem IP, np.: '127.0.0.1'
# 3. Zdefiniuj `host: str` z nazwą hosta, np.: 'localhost'
# 4. Użyj przypisania z rozpakowaniem (unpack assignment)
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> ip
# '127.0.0.1'
#
# >>> host
# 'localhost'

# %% Hints
# - `str.split()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert ip is not Ellipsis, \
'Variable `ip` has an invalid value; assign result of your program to it.'

>>> assert host is not Ellipsis, \
'Variable `host` has an invalid value; assign result of your program to it.'

>>> assert type(ip) is str, \
'Variable `ip` has an invalid type; expected: `str`.'

>>> assert type(host) is str, \
'Variable `host` has an invalid type; expected: `str`.'

>>> result = open(__file__).read()
>>> assert 'ip, host' + ' = ' + 'DATA' in result, \
'Use unpack assignment'

>>> ip
'127.0.0.1'

>>> host
'localhost'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
ip: str
host: str

# %% Data
DATA = '127.0.0.1 localhost'

# %% Result