Nested Dicts

Victor Subervi victorsubervi at gmail.com
Sat Dec 5 12:14:33 EST 2009


On Sat, Dec 5, 2009 at 12:01 PM, Carsten Haese <carsten.haese at gmail.com>wrote:

> 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.
>

Relax, Carsten. Of course I knew about those indentation errors, etc. I was
testing to see if it would even take the line I entered. I wasn't interested
in supplying anything beyond that.

The others who answered this post hit the issue squarely. This "pseudo-code"
was actually supplied to me by one of you python "gurus" who has all my
respect, so I didn't review it as closely as I should have. My apologies.
Thanks,
V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091205/60394e43/attachment.html>


More information about the Python-list mailing list