Note: I have read this post and Alex Martelli's response, but I don't really/fully understand his answer. It's a bit beyond my current understanding. I would like help understanding it better.
I understand that when you try the following for loop:
for key, value in dict:
print key
print value
you get:
ValueError: too many values to unpack
Although you can loop over a dictionary and just get the keys with the following:
for key in dict:
print key
Can anyone provide a slightly less-advanced explanation for why you cannot iterate over a dictionary using key, value without using .iteritems()
?
Python has a feature called iterable unpacking. When you do
a, b = thing
Python assumes thing
is a tuple or list or something with 2 items, and it assigns the first and second items to a
and b
. This also works in a for
loop:
for a, b in thing:
is equivalent to
for c in thing:
a, b = c
except it doesn't create that c
variable.
This means that if you do
for k, v in d:
Python can't look at the fact that you've said k, v
instead of k
and give you items instead of keys, because maybe the keys are 2-tuples. It has to iterate over the keys and try to unpack each key into the k
and v
variables.
The other answer explains it well. But here are some further illustrations for how it behaves, by showing cases where it actually works without error (so you can see something):
>>> d = {(1,2): 3, (4,5): 6}
>>> for k, v in d:
print k, v
1 2
4 5
The loop goes through the keys (1,2)
and (4,5)
and since those "happen to be" tuples of size 2, they can be assigned to k
and v
.
Works with strings as well, as long as they have exactly two characters:
>>> d = {"AB":3, "CD":6}
>>> for k, v in d:
print k, v
A B
C D
I assume in your case it was something like this?
>>> d = {"ABC":3, "CD":6}
>>> for k, v in d:
print k, v
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
for k, v in d:
ValueError: too many values to unpack
Here, the key "ABC" is a triple and thus Python complains about trying to unpack it into just two variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With