Nested Dicts

Carsten Haese carsten.haese at gmail.com
Sat Dec 5 12:01:59 EST 2009


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




More information about the Python-list mailing list