Unpack (expand and assign to multiple variables) tuples and lists in Python

Money and Business

In Python, elements of a tuple or list can be expanded and assigned to multiple variables. This is called sequence unpacking or unpacked assignment.

The following details are described here.

  • Unpacking basics of tuples and lists
  • Nested tuples, unpacked listings
  • Unpacking with Underscores:_
  • Unpacking with asterisks:*

See the following article for information on using asterisks to expand and pass tuples, lists, and dictionaries as function arguments.

Unpacking basics of tuples and lists

When variables are written on the left-hand side, separated by commas, each variable is assigned an element of the tuple or list on the right-hand side. It is the same for both tuples and lists (the following examples are written in tuple form).

t = (0, 1, 2)

a, b, c = t

print(a)
print(b)
print(c)
# 0
# 1
# 2

l = [0, 1, 2]

a, b, c = l

print(a)
print(b)
print(c)
# 0
# 1
# 2

Note that since tuples can omit round brackets, this can be used to assign multiple values to multiple variables on a single line as follows.

a, b = 0, 1

print(a)
print(b)
# 0
# 1

If the number of variables does not match the number of elements, an error occurs.

# a, b = t
# ValueError: too many values to unpack (expected 2)

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

If the number of variables is less than the number of elements, the remaining elements can be assigned as a list by appending an asterisk to the variable name (see below).

Nested tuples, unpacked listings

Nested tuples and lists can also be unpacked. If you want to unpack the contents as well, enclose the variable in one of the following

  • ()
  • []
t = (0, 1, (2, 3, 4))

a, b, c = t

print(a)
print(b)
print(c)
# 0
# 1
# (2, 3, 4)

print(type(c))
# <class 'tuple'>

a, b, (c, d, e) = t

print(a)
print(b)
print(c)
print(d)
print(e)
# 0
# 1
# 2
# 3
# 4

Unpacked with _underscore_.

In Python, not only unpacked, values that are not needed are conventionally assigned to the underscore (underscore) _. There is no special grammatical meaning; they are simply assigned to a variable named _.

t = (0, 1, 2)

a, b, _ = t

print(a)
print(b)
print(_)
# 0
# 1
# 2

Unpacking with asterisks

If the number of variables is less than the number of elements, an asterisk in the variable name will cause the elements to be assigned together as a list.

This syntax has been implemented since Python 3 and is not available in Python 2.

The elements are assigned from the beginning and the end to the variables without asterisks, and the remaining elements are assigned as a list to the variables with asterisks.

t = (0, 1, 2, 3, 4)

a, b, *c = t

print(a)
print(b)
print(c)
# 0
# 1
# [2, 3, 4]

print(type(c))
# <class 'list'>

a, *b, c = t

print(a)
print(b)
print(c)
# 0
# [1, 2, 3]
# 4

*a, b, c = t

print(a)
print(b)
print(c)
# [0, 1, 2]
# 3
# 4

For example, if you want to assign only the first two elements of a tuple or list to a variable, you might use the above underscore for the parts that are not needed.

a, b, *_ = t

print(a)
print(b)
print(_)
# 0
# 1
# [2, 3, 4]

The same can also be written as follows

a, b = t[0], t[1]

print(a)
print(b)
# 0
# 1

Only one asterisk can be attached. If there are multiple variables marked with an asterisk, a SyntaxError error will result because it is not possible to determine how many elements are assigned to each variable.

# *a, b, *c = t
# SyntaxError: two starred expressions in assignment

Note that even a single element assigned to a variable marked with an asterisk is assigned as a list.

t = (0, 1, 2)

a, b, *c = t

print(a)
print(b)
print(c)
# 0
# 1
# [2]

print(type(c))
# <class 'list'>

If there are no extra elements, an empty list is assigned.

a, b, c, *d = t

print(a)
print(b)
print(c)
print(d)
# 0
# 1
# 2
# []
Copied title and URL