Too Many Values To Unpack
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Nov 22 00:37:17 EST 2009
On Sat, 21 Nov 2009 21:06:08 -0800, Dennis Lee Bieber wrote:
> I apparently thought "for ... in dictionary" would return (key,
> value) pairs, but it appears that it only returns the key itself -- and
> a single key can't be unpacked.
>
> Misleading error... too many /targets/ to unpack...
No, the error is fine.
>>> for x, y in {1:'a'}:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unpack non-sequence
>>>
>>> for x, y in {'a':1}:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
>>>
>>> for x, y in {'parrot':1}:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
Strings are iterable, and so unpack into individual characters.
--
Steven
More information about the Python-list
mailing list