How to solve valueerror: too many values to unpack (expected 2)
Posted on: March 15, 2021 by Deven
In this article, you will learn how to solve valueerror: too many values to unpack (expected 2) error in Python.
Let’s look at a code example that produces the same error.
fruits = {
"name": "mago",
"buy_quantity": 2,
"price": 20
}
for key, value in fruits:
print("Key:", key)
print("Value:", str(value))
output
Traceback (most recent call last):
File "<string>", line 6, in <module>
ValueError: too many values to unpack (expected 2)
In order to solve valueerror: too many values to unpack (expected 2) error we use the items()
method to iterate over a dictionary. Consider the example below:
fruits = {
"name": "mago",
"buy_quantity": 2,
"price": 20
}
for key, value in fruits.items():
print("Key:", key)
print("Value:", str(value))
output:
Key: name
Value: mago
Key: buy_quantity
Value: 2
Key: price
Value: 20
Share on social media
//