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


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

Proper deletion of selected items during map iteration in for loop

Started byCharles Hixson <charleshixsn@earthlink.net>
First post2014-04-25 10:53 -0700
Last post2014-04-25 10:53 -0700
Articles 1 — 1 participant

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


Contents

  Proper deletion of selected items during map iteration in for loop Charles Hixson <charleshixsn@earthlink.net> - 2014-04-25 10:53 -0700

#70607 — Proper deletion of selected items during map iteration in for loop

FromCharles Hixson <charleshixsn@earthlink.net>
Date2014-04-25 10:53 -0700
SubjectProper deletion of selected items during map iteration in for loop
Message-ID<mailman.9503.1398448502.18130.python-list@python.org>
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?

-- 
Charles Hixson

[toc] | [standalone]


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


csiph-web