Too Many Values To Unpack

Carsten Haese carsten.haese at gmail.com
Fri Nov 20 11:32:32 EST 2009


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




More information about the Python-list mailing list