del not working for (exhausted) dict iterable value (Python 3.3)

alex23 wuwei23 at gmail.com
Tue Mar 12 06:52:15 CET 2013


On Mar 12, 3:35 pm, Nick Mellor <thebalance... at gmail.com> wrote:
> event['Items'] is an exhausted (all used up) iterable.
>
> Now I do the following (lines 142-4):
>
>             event.update({'Attributes': filtered_attributes})
>             del event['Items']
>             yield event
>
> and get a KeyError on the del statement.
>
> Is there some deep design in Python here, that it won't delete a
> dict value that's an (exhausted) iterator, or have I found a bug?

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.



More information about the Python-list mailing list