. Advertisement .
..3..
. Advertisement .
..4..
I get the ”not enough values to unpack (expected 2, got 1)” error when i run my program.
In order to add each key/value pair to a Python dictionary, I need to create the function add_to_dict(d, key_value_pairs)
. A list of tuples with the type key_value_pairs
will make up the argument (key, value)
.
A list of all the key/value pairs that have changed should be returned by the function (with their original values).
def add_to_dict(d, key_value_pairs):
newlist = []
for key,value in d:
for x,y in key_value_pairs:
if x == key:
newlist.append(x,y)
return newlist
And then I get the error message:
ValueError: not enough values to unpack (expected 2, got 1)
Can someone give me some advice?
The cause: I think this error happens because you iterated over the dict (of 1 million entries).
Solution: To fix this error, you can’t iterate over the dict (of 1 million entries) but only over the list of possible changes and see if it changes anything in the dict:
Output:
You might also add some parameter validation; if any parameter is incorrect, nothing will be done and a spoken error would result:
This results in clearer error messages: