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

N

Nick Mellor

Hi all,

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. 'Items' is a key in the event dictionary at this point.

The full file is here:

https://gist.github.com/nickmellor/5140516

I can get round it by copying all but the 'Items' member to a new dictionary, but obviously I don't want to.

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?

Thanks for any help,

Nick
 
A

alex23

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

Thomas Rachel

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.


Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top