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


Groups > comp.lang.python > #70609

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

References <535AA12A.1030203@earthlink.net>
Date 2014-04-26 04:00 +1000
Subject Re: Proper deletion of selected items during map iteration in for loop
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.9505.1398448862.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

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

csiph-web