Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; '"to': 0.16; '(k,': 0.16; 'charles': 0.16; 'deletions': 0.16; 'expected):': 0.16; 'iterating': 0.16; 'iteration': 0.16; 'message- id:@earthlink.net': 0.16; 'received:dsl.mindspring.com': 0.16; 'subject: \n ': 0.16; 'surprising': 0.16; 'wrote:': 0.18; 'separate': 0.22; 'header:User-Agent:1': 0.23; "shouldn't": 0.24; 'looks': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'am,': 0.29; "doesn't": 0.30; 'gives': 0.31; 'keys': 0.31; 'way?': 0.31; 'subject:all': 0.32; 'received:66': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'in.': 0.36; "i'll": 0.36; 'should': 0.36; 'to:addr:python- list': 0.38; 'list,': 0.38; 'expect': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'deleting': 0.60; 'new': 0.61; 'back': 0.62; 'save': 0.62; 'answer.': 0.68; 'saving': 0.69 Date: Sat, 26 Apr 2014 12:25:27 -0700 From: Charles Hixson User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Proper deletion of selected items during map iteration in for loop: Thanks to all References: <535AA12A.1030203@earthlink.net> In-Reply-To: <535AA12A.1030203@earthlink.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398540343 news.xs4all.nl 2909 [2001:888:2000:d::a6]:36400 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70639 On 04/25/2014 10:53 AM, Charles Hixson 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? > Going over the various responses, it looks like saving the "to be deleted" keys to a list, and then iterating over that to delete is the best answer. I expect that I'll be deleting around 1/3 during each iteration of the process...and then adding new ones back in. There shouldn't be a really huge number of deletions on any particular pass, but it will be looped through many times...so if there were any better way to do this, it would speed things up considerably...but it's not really surprising that there doesn't appear to be. So now it translates into (approximately, not yet tested): toDel = [] for (k, v) in m.items(): if f(k): # do some processing of v and save result elsewhere toDel.append(k) else: # do something else for k in toDel: del m[k] toDel = None -- Charles Hixson