dictionary size changed during iteration
Hans Mulder
hansmu at xs4all.nl
Sun May 8 15:32:06 EDT 2011
On 08/05/2011 00:12, Roy Smith wrote:
> In article<7xd3jukyn9.fsf at ruckus.brouhaha.com>,
> Paul Rubin<no.email at nospam.invalid> wrote:
>
>> Roy Smith<roy at panix.com> writes:
>>> changes = [ ]
>>> for key in d.iterkeys():
>>> if is_bad(key):
>>> changes.append(key)
>>
>> changes = list(k for k in d if is_bad(k))
>>
>> is a little bit more direct.
>
> This is true. I still file list comprehensions under "new fangled
> toys". While I use them, and appreciate their value, I admit they're
> not always the first thing that comes to my mind.
>
> OBTW,
>
>> changes = [k for k in d if is_bad(k)]
>
> is even more direct :-)
How about:
changes = filter(is_bad, d)
Or would that be too compact?
-- HansM
More information about the Python-list
mailing list