Proper deletion of selected items during map iteration in for loop

Tim Chase python.list at tim.thechases.com
Fri Apr 25 15:22:59 EDT 2014


On 2014-04-25 14:50, Terry Reedy wrote:
> If you expect to delete more than half the keys *and* if there are
> no other references to the dict, such that you need the particular
> object mutated, this might be better.

If that's your precondition, then it might be better to do something
like

  keep = {}
  for (k, v) in m.items():
    if f(k):
      #  do some processing of v and save result elsewhere
    else:
      keep[k] = v
  m.clear()
  m.update(keep)
  del keep  # optionally free this back up when done

This would allow things that refer to "m" to retain the same
reference, but should have the same result as deleting them without
having to duplicate what the OP describes as a large volume of data.

Either way, the options are mostly

1) clone the entire .items() into a list, and iterate over that for
deleting from your underlying view (what Chris & Matthew suggest)

2) iterate over the items, storing up the ones to delete and delete
them all at the end (what the OP mentions having tried before)

3) iterate over the items, storing up the ones to keep, delete the
rest, and then put the kept ones back (what I code above)

The choice between #2 and #3 hinge on whether you expect to delete
more than half the items.  If you plan to delete more than half, store
the ones to keep (#3); if you plan to delete less than half, store
the ones to delete (#2).

-tkc








More information about the Python-list mailing list