ValueError: too many values to unpack
The "ValueError: too many values to unpack" in Python occurs when you try to unpack more values than there are variables on the left side of the assignment. This typically happens with unpacking sequences like tuples, lists, or dictionaries. Let's see some examples and how to resolve them:
Unpacking More Values
In this example, you are trying to unpack a tuple containing three values into two variables a and b. Python raises a "ValueError: too many values to unpack" because there are more values in the tuple than there are variables to assign them.

To fix this, make sure the number of variables matches the number of values in the sequence you are unpacking:
Unpacking Nested Sequence
In this example, you are trying to unpack a list containing two elements, one of which is a nested list [2, 3], into two variables x and y. Python raises a "ValueError: too many values to unpack" because you are trying to assign the entire nested list to a single variable y.
To fix this, ensure you have the right number of variables for each level of nesting in the sequence:
Unpacking Dictionary
In this example, you are trying to unpack a dictionary into two variables a and b. Python raises a "ValueError: too many values to unpack" because dictionaries do not have a fixed order, and you cannot guarantee the order of key-value pairs during unpacking.
To fix this, use appropriate methods like dict.items() or dict.keys() to access key-value pairs or keys respectively:
Conclusion
The "ValueError: too many values to unpack" in Python can be resolved by ensuring that the number of variables on the left side of the assignment matches the number of values in the sequence being unpacked. If dealing with nested sequences or dictionaries, ensure that the unpacking pattern matches the structure of the sequence or dictionary.
- TypeError: 'NoneType' object is not subscriptable
- IndexError: string index out of range
- IndentationError: unexpected indent Error
- SyntaxError- EOL while scanning string literal
- TypeError: Can't convert 'int' object to str implicitly
- IndentationError: expected an indented block
- ValueError: invalid literal for int() with base 10
- IndexError: list index out of range : Python
- AttributeError: 'module' object has no attribute 'main'
- UnboundLocalError: local variable referenced before assignment
- TypeError: string indices must be integers
- FileNotFoundError: [Errno 2] No such file or directory
- Fatal error: Python.h: No such file or directory
- ZeroDivisionError: division by zero
- ImportError: No module named requests | Python
- TypeError: 'NoneType' object is not iterable
- SyntaxError: unexpected EOF while parsing | Python
- zsh: command not found: python
- Unicodeescape codec can't decode bytes in position 2-3
- The TypeError: 'tuple' object does not support item assignment
- The AttributeError: 'bytes' object has no attribute 'read'