Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41160

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

From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: del not working for (exhausted) dict iterable value (Python 3.3)
Date 2013-03-13 05:02 +0100
Organization A newly installed InterNetNews server
Message-ID <khotp3$sja$1@r03.glglgl.gl> (permalink)
References <2879f537-1666-46f7-abbf-38204d8184c2@googlegroups.com> <0809a262-494f-46d6-910a-909a06e2aa92@m9g2000pby.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

del not working for (exhausted) dict iterable value (Python 3.3) Nick Mellor <thebalancepro@gmail.com> - 2013-03-11 22:35 -0700
  Re: del not working for (exhausted) dict iterable value (Python 3.3) alex23 <wuwei23@gmail.com> - 2013-03-11 22:52 -0700
    Re: del not working for (exhausted) dict iterable value (Python 3.3) Nick Mellor <thebalancepro@gmail.com> - 2013-03-12 15:40 -0700
    Re: del not working for (exhausted) dict iterable value (Python 3.3) Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2013-03-13 05:02 +0100

csiph-web