{ }codelper

Error Messages › Python

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))
Input
3
5
When you run it
Traceback (most recent call last):
  File "main.py", line 1, in <module>
ValueError: not enough values to unpack (expected 2, got 1)

Edit and run it →

Fixed code

If there is one value per line, call input() once for each line.

a = int(input())b = int(input())print(a + b)
Input
3
5
Output
8

Edit and run it →

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))
Input
3,5
When you run it
Traceback (most recent call last):
  File "main.py", line 1, in <module>
ValueError: not enough values to unpack (expected 2, got 1)

Edit and run it →

Fixed code

Put the separator character inside split().

a, b = input().split(",")print(int(a) + int(b))
Input
3,5
Output
8

Edit and run it →

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))
Input
mina 90
jun
When you run it
mina 90
Traceback (most recent call last):
  File "main.py", line 2, in <module>
ValueError: not enough values to unpack (expected 2, got 1)

Edit and run it →

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")
Input
mina 90
jun
Output
mina 90
jun no score

Edit and run it →

What to check when you see this error

  1. Read the number after expected (how many variables) and the number after got (how many values there really were) in the error message.
  2. On the line where the error happened (the number after line), print the value on the right of = with print(). If it is input().split(), store it with parts = input().split() and run print(parts).
  3. Check the problem's input format again: are there several values on one line, or one per line?
  4. 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