del not working for (exhausted) dict iterable value (Python 3.3)
nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Wed Mar 13 05:02:42 CET 2013
Am 12.03.2013 06:52 schrieb alex23:
> You're effectively doing this:
>>>> event = dict(Items=[1,2,3])
>>>> for e in event['Items']:
> ... del event['Items']
> Traceback (most recent call last):
> File "<stdin>", line 2, in <module>
> KeyError: 'Items'
> You want to move your del statement up an indentation level so it
> happens after the iterator is actually exhausted, and not after the
> first iteration.
Just to be clear: Exhausting the iterator is not the problem, as I
thought as well at the first glance.
The problem is the fact that the loop body tuns multiple times - and so
does the del statement. A
event = dict(Items=[1,2,3])
for e in event['Items']:
if 'Items' in event: del event['Items']
runs perfectly, as the iterable is transformed to an iterator at the
very start of the loop.
More information about the Python-list