Fix: ValueError: not enough values to unpack
Exact message
ValueError: not enough values to unpack (expected 2, got 1)
🛠️ Fix it right here with Python Error Fixer
Edit the traceback below if needed, then let our AI suggest a fix without leaving this page.
Quick answer
This error occurs when you try to assign fewer values from an iterable (like a list or tuple) to more variables than it contains. For example, trying to unpack a list with 1 item into 2 variables.
Why this error happens
Python requires the number of variables on the left side of the = sign to exactly match the number of items in the iterable on the right side. If the iterable returns fewer items than expected (e.g., a function returns 1 item instead of 2), this error is raised.
Example: Wrong vs Right
Wrong
my_list = [1] a, b = my_list # ValueError: not enough values to unpack (expected 2, got 1)
Right
my_list = [1, 2]
a, b = my_list
# Or, if the list size varies, use a default or check length:
if len(my_list) == 2:
a, b = my_listHow to fix it (step by step)
- Check the length of the iterable before unpacking it using len().
- Ensure the function returning the data is actually returning the number of items you expect.
- If unpacking a string, remember that spaces count as characters.
- If you are unpacking a dictionary, use .items() to get keys and values together.
Frequently asked questions
Why does this happen with a string?
Strings are iterables. If you do a, b = 'hello', Python tries to unpack 5 characters into 2 variables, which fails.
What if the list has more items than variables?
You will get 'too many values to unpack'. You can use the * (star) operator to catch the rest: a, *b = [1, 2, 3].
Related Python errors
Need a fix for a different error?
Use the free to paste any traceback and get an AI-suggested fix in seconds.
Still stuck on this error?
Paste your full traceback and our AI will fix it for free.
Want an AI with memory for your project?
MonsterX AI remembers your codebase across chats.