Skip to main content

How to Unpack Values into Variables in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Python can take a list or a tuple apart and put each value into its own variable in one line: year, month, day = "2026-11-28".split("-"). This is called unpacking, and an underscore stands in for a value you don't need.

Syntax​

Syntax
<name>, <name>, <name> = <sequence>
<name>, _, <name> = <sequence>
PartWhat it does
<name>, <name>, ...One variable for each value in the sequence, in order.
<sequence>A list, a tuple, a string or anything else that has exactly that many values.
_An ordinary name that, by convention, means "I don't need this value".

The Python tutorial on tuples and sequences: "This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence."

The Example​

The example unpacks a date split at its dashes into three variables, unpacks a tuple while skipping its middle value with _, and then tries to unpack three names into two variables.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which unpacks the split date into year, month and day, unpacks a tuple into name, underscore and city, then unpacks three split names into first and last.

main.py
year, month, day = "2026-11-28".split("-")
print(year, month, day)

name, _, city = ("Ada", 36, "London")
print(name, city)

first, last = "Ada Lovelace Byron".split()

Step 2: Run It​

Terminal
uv run python main.py
Output
2026 11 28
Ada London
Traceback (most recent call last):
File "/Users/Shared/you/main.py", line 7, in <module>
first, last = "Ada Lovelace Byron".split()
^^^^^^^^^^^
ValueError: too many values to unpack (expected 2, got 3)

A zsh terminal where uv run python main.py prints 2026 11 28 and Ada London, then a traceback ending in ValueError: too many values to unpack, expected 2, got 3.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • The split date had three pieces, so year, month and day each got one.
  • The tuple's middle value, 36, went into _, and the code never used it.
  • "Ada Lovelace Byron".split() has three pieces but only two names were waiting for them, so Python stopped with a ValueError that names both numbers.

Common Mistakes​

MistakeWhat happensDo this instead
More values than namesValueError: too many values to unpack (expected 2, got 3)Add a name, or collect the rest with a star: first, *rest = ...
Fewer values than namesValueError: not enough values to unpack (expected 3, got 2)Check how many pieces the data really has
Reusing _ later as a real variableIt holds whatever was unpacked last, which is easy to misreadKeep _ for values you throw away

Troubleshooting​

SymptomLikely causeFix
ValueError: too many values to unpackThe sequence is longer than the list of namesCount the pieces, for example by printing the list first
ValueError: not enough values to unpackThe sequence is shorter, for example a split that found fewer separatorsCheck the input before unpacking

FAQ​

How do I unpack the first value and keep the rest?​

Put a star before the last name: first, *rest = [1, 2, 3, 4] gave first the value 1 and rest the list [2, 3, 4].

Is the underscore special?​

No. _ is a normal variable name; using it for unwanted values is a convention that other Python programmers recognise.

Conclusion​

a, b, c = sequence puts each value into its own variable, as long as the counts match exactly. Use _ for a value you don't need, and a starred name to collect any extras.

Additional Resources​