Python ValueError: not enough values to unpack
ValueError: not enough values to unpack (expected 2, got 1)
When you spread values across several variables at once, as in a, b = values, this error appears if there are fewer values on the right than variables on the left. expected 2 means there are 2 variables, and got 1 means there was only 1 value. Most of the time, input().split() gave you fewer pieces than you thought.
Cause 1. The numbers are one per line
input().split() reads and splits one line only. If the input is 3 and 5 on two lines, the first line has just one value.
Wrong code
a, b = input().split()print(int(a) + int(b))3 5
Traceback (most recent call last): File "main.py", line 1, in <module> ValueError: not enough values to unpack (expected 2, got 1)
Fixed code
If there is one value per line, call input() once for each line.
Cause 2. The values are separated by something other than spaces
With empty parentheses, split() splits at spaces. If the input is separated by a comma, like 3,5, there is nowhere to split, so you get a single piece, "3,5".
Wrong code
a, b = input().split()print(int(a) + int(b))3,5
Traceback (most recent call last): File "main.py", line 1, in <module> ValueError: not enough values to unpack (expected 2, got 1)
Fixed code
Put the separator character inside split().
Cause 3. One line is missing a value
When you read many lines, some of them may hold fewer values. The earlier lines work fine and the error happens on that line. For an empty line, the message says got 0.
Wrong code
for i in range(2): name, score = input().split() print(name, int(score))mina 90 jun
mina 90 Traceback (most recent call last): File "main.py", line 2, in <module> ValueError: not enough values to unpack (expected 2, got 1)
Fixed code
Read the line into a list first, check how many items it has, and only then unpack it.
for i in range(2): parts = input().split() if len(parts) == 2: name, score = parts print(name, int(score)) else: print(parts[0], "no score")mina 90 jun
mina 90 jun no score
What to check when you see this error
- Read the number after
expected(how many variables) and the number aftergot(how many values there really were) in the error message. - On the line where the error happened (the number after
line), print the value on the right of=withprint(). If it isinput().split(), store it withparts = input().split()and runprint(parts). - Check the problem's input format again: are there several values on one line, or one per line?
- If there are too many values instead, you get
too many values to unpack (expected 2). The fix is the same: make the counts match.
Practice problems
See also
- Python ValueError: invalid literal for int()
- Python EOFError: EOF when reading a line
- Python IndexError: list index out of range
- Python TypeError: 'int' object is not iterable
- Syntax: Python split(): Split a String into a List
- Syntax: Python input(): Read User Input
- Learn it step by step: Python Reading input
- All Python error messages
- Online Python interpreter