How to Unpack a Tuple or List in Python

Modified: | Tags: Python, List

In Python, you can assign elements of a tuple or list to multiple variables. This process is known as sequence unpacking.

See the following article for information on unpacking lists, tuples, and dictionaries (dict) as arguments in a function using * (asterisk).

Basics of tuple and list unpacking

You can assign elements from a tuple or list to multiple variables by placing comma-separated variables on the left-hand side.

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

The following examples use tuples, but the same approach applies to lists.

Since parentheses around tuples can be omitted, you can assign multiple values to variables in a single line, as shown below.

a, b = 0, 1

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

A ValueError occurs if the number of variables does not match the number of elements.

# 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 there are fewer variables than elements, you can prefix one variable with an asterisk * to collect the remaining elements in a list. This will be explained in more detail later.

Unpack a nested tuple or list

You can also unpack a nested tuple or list. To unpack inner elements, use parentheses () or brackets [] to mirror the nested structure.

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

Unpack with _ (underscore)

In Python, by convention, _ (underscore) is often used to indicate values that are intentionally unused. It has no special syntactic meaning and is just a regular variable name.

t = (0, 1, 2)

a, b, _ = t

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

Unpack with * (asterisk)

When unpacking, you can prefix one variable with an asterisk * to collect any remaining elements into a list.

This feature is available in Python 3 and is not supported in Python 2.

Individual elements are assigned to regular variables before and after the *-prefixed variable, while all remaining elements are collected into the *-prefixed variable as a list.

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, to assign only the first two elements of a tuple or list to variables, you can use the *-prefixed underscore (*_) to ignore the remaining elements.

a, b, *_ = t

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

The same operation can be written as follows.

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

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

Only one variable can be prefixed with *. Using more than one causes ambiguity and results in a SyntaxError.

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

Even if only one element is collected by a *-prefixed variable, it is still 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
# []

Related Categories

Related Articles