Proper deletion of selected items during map iteration in for loop

Chris Angelico rosuav at gmail.com
Fri Apr 25 14:00:53 EDT 2014


On Sat, Apr 26, 2014 at 3:53 AM, Charles Hixson
<charleshixsn at 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



More information about the Python-list mailing list