Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Too Many Values To Unpack

31 views
Skip to first unread message

Carsten Haese

unread,
Nov 20, 2009, 11:32:32 AM11/20/09
to pytho...@python.org
Victor Subervi wrote:
> Hi;
> At one point Dennis Lee Bieber helped me with the following slightly
> modified code:
>
> [snippage...]
>
> def getChildren(levelDict, level = 0):
> MAXLEVEL = 7
> if level > MAXLEVEL:
> return #possibly the data has a cycle/loop
> for (nm, dt) in levelDict:
> cursor.execute('''select c.name <http://c.name> from categories as c
> inner join relationship as r
> on c.ID = r.Child
> inner join categories as p
> on r.Parent = p.ID
> where p.category = %s
> order by c.name <http://c.name>''', (nm,))
> levelDict[nm] = expand(cursor.fetchall())
> # recursive call to do next level
> getChildren(levelDict[nm], level + 1)
> # no data return as we are mutating dictionaries in place
>
> [snippage...]
>
> [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] for (nm,
> dt) in levelDict:
> [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] ValueError:
> too many values to unpack
Iterating over a dictionary, such as what you're doing in the line <<for
(nm, dt) in levelDict>>, only produces the keys that are in the
dictionary. This means that this line only works if the keys in
levelDict are pairs of "nm"s and "dt"s, whatever "nm" and "dt"
represent. (In case the foregoing is too subtle a clue: Choose better
variable names.)

However, the line <<levelDict[nm] = expand(...)>> indicates that the
keys in levelDict are only "nm"s, which are presumably single objects,
not pairs. Also, the "dt" that you're trying to unpack from levelDict's
keys is not used anywhere in your function.

So, this would indicate that changing the offending line to <<for nm in
levelDict>> should fix this particular error.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net

Carsten Haese

unread,
Nov 20, 2009, 1:07:49 PM11/20/09
to pytho...@python.org
Victor Subervi wrote:
> Now what I don't understand about the above code, provided by Dennis Lee
> Bieber, is the "expand" statement.

It's not a statement. It's a function. The function is defined in your
code. Or rather, it's defined in the code that you copied from Dennis
Lee Bieber, apparently. Look at the function definition. It'll tell you
what the function does.

> Could
> someone please explain what this code does?

Maybe you should ask the person that wrote the code.

Message has been deleted

Carsten Haese

unread,
Nov 21, 2009, 4:41:04 PM11/21/09
to pytho...@python.org
Victor Subervi wrote:
> File "/var/www/html/angrynates.com/cart/catTree.py
> <http://angrynates.com/cart/catTree.py>", line 25, in getChildren

> for (nm, dt) in levelDict:
> ValueError: too many values to unpack
>
> Please advise.

I already explained what's causing this error. Read my first response on
this thread.
(http://mail.python.org/pipermail/python-list/2009-November/1227008.html)

Message has been deleted

Gregory Ewing

unread,
Nov 22, 2009, 12:19:49 AM11/22/09
to
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...

My guess is that the keys are strings, which means it's
unpacking them into characters, in which case a key of
length 3 or more will produce that message.

--
Greg

Steven D'Aprano

unread,
Nov 22, 2009, 12:37:17 AM11/22/09
to
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

0 new messages