Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'odd': 0.07; 'string': 0.09; '"__main__":': 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'def': 0.12; '(k,': 0.16; '[])': 0.16; 'charles': 0.16; 'data)': 0.16; 'expected):': 0.16; 'first:': 0.16; 'iteration': 0.16; 'keys:': 0.16; 'lasts': 0.16; 'prev': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'struck': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'import': 0.22; 'separate': 0.22; 'header :User-Agent:1': 0.23; "i've": 0.25; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'gives': 0.31; 'keys': 0.31; 'way?': 0.31; 'probably': 0.32; 'except': 0.35; 'but': 0.35; 'there': 0.35; 'should': 0.36; 'to:addr:python-list': 0.38; 'list,': 0.38; 'delete': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'most': 0.60; 'break': 0.61; 'first': 0.61; 'save': 0.62; 'minutes': 0.67; 'effective.': 0.68; 'feeling': 0.68; 'dict.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Proper deletion of selected items during map iteration in for loop Date: Sat, 26 Apr 2014 23:30:57 +0200 Organization: None References: <535AA12A.1030203@earthlink.net> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9ecc.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 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: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398547876 news.xs4all.nl 2908 [2001:888:2000:d::a6]:58176 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70641 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? It just struck me that you can store the keys to be deleted as values in the same dict. That way you need no extra memory: def delete_items(d, keys): keys = iter(keys) try: first = prev = next(keys) except StopIteration: return for key in keys: d[prev] = prev = key d[prev] = first key = first while True: key = d.pop(key) if key is first: break if __name__ == "__main__": import string data = dict(zip(range(10), string.ascii_lowercase)) print("original data:", data) print("removing odd items...") delete_items(data, keys=(k for k in data if k % 2)) print("modified data:", data) print("delete no items...") delete_items(data, []) print("modified data:", data) print("delete a single item (6)...") delete_items(data, [6]) print("modified data:", data) print("delete all items...") delete_items(data, data) print("modified data:", data) While I think I am a genius* in practice this approach will probably not be the most effective. (*) (Un)fortunately that feeling never lasts longer than a few minutes ;)