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


Groups > comp.lang.python > #70609 > unrolled thread

Re: Proper deletion of selected items during map iteration in for loop

Started byChris Angelico <rosuav@gmail.com>
First post2014-04-26 04:00 +1000
Last post2014-04-26 04:00 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Proper deletion of selected items during map iteration in for loop Chris Angelico <rosuav@gmail.com> - 2014-04-26 04:00 +1000

#70609 — Re: Proper deletion of selected items during map iteration in for loop

FromChris Angelico <rosuav@gmail.com>
Date2014-04-26 04:00 +1000
SubjectRe: Proper deletion of selected items during map iteration in for loop
Message-ID<mailman.9505.1398448862.18130.python-list@python.org>
On Sat, Apr 26, 2014 at 3:53 AM, Charles Hixson
<charleshixsn@earthlink.net> wrote:
> What is the proper way to delete selected items during iteration of a map?
> What I want to do is:
>
> for (k, v) in m.items():
>    if f(k):
>       #  do some processing of v and save result elsewhere
>       del m[k]
>
> But this gives (as should be expected):
>         RuntimeError: dictionary changed size during iteration
> In the past I've accumulated the keys to be deleted in a separate list, but
> this time there are likely to be a large number of them, so is there some
> better way?

One easy way is to explicitly coalesce the items() view into a list:

for k, v in list(m.items()):

That would work, but you effectively duplicate your entire dictionary
into a list. More likely, you should simply snapshot the keys:

for k in list(m):
    # Your example code isn't using v, but I
    # assume the real code does
    v = m[k]
    if f(k):
        # as above
        del m[k]

This and your previous technique of accumulating a delete-me list
would be the two standard ways to filter a dictionary with a loop. (I
say "with a loop" because there's a third way to filter a dictionary,
and that's with a comprehension. But I don't know of a really clean
way to save v elsewhere for the cases where the dict isn't keeping
that key.)

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web