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

Re: Nested Dicts

0 views
Skip to first unread message

D'Arcy J.M. Cain

unread,
Dec 5, 2009, 11:24:40 AM12/5/09
to Victor Subervi, python-list
On Sat, 5 Dec 2009 10:52:10 -0500
Victor Subervi <victor...@gmail.com> wrote:
> Hi;
> I have the following error:
>
> /var/www/html/angrynates.com/cart/catTree.py in
> getChildren(levelDict={'cat3': {}}, level=0)
> 23 if level > MAXLEVEL:
> 24 return #possibly the data has a cycle/loop
> 25 for (nm, dt) in levelDict: ### ERROR HERE

Yes, you are tring to assign to two variabels but levelDict is a single
object, a dictionary with a single element which is another
dictionary. If, as I assume, nm and dt (very poor variable names,
especially when you are asking others to help you debug) refer to name
and data by which you mean key and value from the dictionary then
perhaps you really want this.

for key, val in levelDict.items():
...

--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

MRAB

unread,
Dec 5, 2009, 11:25:03 AM12/5/09
to python-list
Victor Subervi wrote:
> Hi;
> I have the following error:
>
> /var/www/html/angrynates.com/cart/catTree.py
> <http://angrynates.com/cart/catTree.py> in
> getChildren(levelDict={'cat3': {}}, level=0)
> 23 if level > MAXLEVEL:
> 24 return #possibly the data has a cycle/loop
> 25 for (nm, dt) in levelDict: ### ERROR HERE
> 26 cursor.execute('''select c.name <http://c.name> from
> categories as c
> 27 inner join relationship as r
> nm undefined, dt undefined, levelDict = {'cat3': {}}
>
> ValueError: too many values to unpack
> args = ('too many values to unpack',)
>
> However...
>
> >>> d = {'cat': {'one':'two'}}
> >>> for a, b in d:
> ...
> File "<stdin>", line 2
>
> ^
> IndentationError: expected an indented block
> >>> d = {'cat': {}}
> >>> for a, b in d:
> ...
> File "<stdin>", line 2
>
> ^
> IndentationError: expected an indented block
> >>>
>
> So apparently, if either the nested dict is populated or not doesn't
> seem to throw any errors in the interpreter. What, then, is the nature
> of this error?
>
When you iterate through a dict, it yields the keys. It's all in the
documentation.

Carsten Haese

unread,
Dec 5, 2009, 12:01:59 PM12/5/09
to pytho...@python.org
Victor Subervi wrote:
>>>> d = {'cat': {'one':'two'}}
>>>> for a, b in d:
> ...
> File "<stdin>", line 2
>
> ^
> IndentationError: expected an indented block
>>>> d = {'cat': {}}
>>>> for a, b in d:
> ...
> File "<stdin>", line 2
>
> ^
> IndentationError: expected an indented block
>>>>
>
> So apparently, if either the nested dict is populated or not doesn't
> seem to throw any errors in the interpreter.

What do you mean by "doesn't seem to throw any errors in the
interpreter?" Can't you see the IndentationErrors above?!?
IndentationErrors *are* errors!

A for-loop needs to have at least one statement inside the suite after
the colon. You're not putting any statements after the colon, and that's
why Python is throwing the same error in either case.

Because you're not entering syntactically correct code, the interpreter
session never even gets around to iterating over the dictionary, so your
session dies before it gets a chance to reproduce the error you're
looking for. If you put a dummy "pass" statement into the loop, you'll
reproduce the ValueError you're trying to troubleshoot:

>>> d = {'cat': {'one':'two'}}
>>> for a, b in d:

... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>


ValueError: too many values to unpack

As far as what's causing this error, I already explained that two weeks
ago: http://groups.google.com/group/comp.lang.python/msg/b9e02a9a9b550ad3

The fact that you're still struggling with this error is deeply
disturbing to me.

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

Carsten Haese

unread,
Dec 5, 2009, 12:39:17 PM12/5/09
to pytho...@python.org
Victor Subervi wrote:
> Of course I knew about those indentation errors

That may be so, but you cleverly disguised this fact by saying the exact
opposite.

0 new messages