Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'example:': 0.03; 'formed': 0.07; 'subject:changing': 0.07; 'terry': 0.07; 'dict': 0.09; 'exception.': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.14; 'wrote:': 0.14; 'angelico': 0.16; 'dictionary,': 0.16; 'iterable,': 0.16; 'keys.': 0.16; 'reedy': 0.16; 'possibly': 0.16; 'mon,': 0.17; 'jan': 0.20; '(which': 0.20; 'header:In-Reply-To:1': 0.21; 'trying': 0.23; 'keys': 0.23; 'correctly.': 0.25; 'do,': 0.25; 'function': 0.25; "i'm": 0.27; 'elements': 0.29; 'explicitly': 0.29; 'subject:How': 0.30; 'iterating': 0.30; 'key,': 0.30; 'header:X-Complaints-To:1': 0.32; 'to:addr:python- list': 0.33; 'break': 0.33; 'list': 0.33; 'chris': 0.34; 'done.': 0.34; 'header:User-Agent:1': 0.35; 'takes': 0.37; '20,': 0.37; 'received:org': 0.38; 'but': 0.38; 'back.': 0.38; 'creates': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'easier': 0.39; 'header:Mime- Version:1': 0.39; 'to:addr:python.org': 0.39; 'entirely': 0.40; 'delete': 0.40; 'pop': 0.65; 'cause': 0.67; '12:32': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: How to iterate on a changing dictionary Date: Sun, 19 Jun 2011 12:02:12 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 82.94.164.166 X-Trace: 1308499346 news.xs4all.nl 49041 [::ffff:82.94.164.166]:49030 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7976 On 6/19/2011 11:13 AM, Chris Angelico wrote: > On Mon, Jun 20, 2011 at 12:32 AM, TheSaint wrote: >> Hello >> >> Trying to pop some key from a dict while is iterating over it will cause an >> exception. >> How I can remove items when the search result is true. >> >> Example: >> >> while len(dict): >> for key in dict.keys(): >> if dict[key] is not my_result: >> dict.pop(key) >> else: >> condition_to_break >> print('Dictionary is over') > > One way is to iterate over an explicitly formed list of the keys. > > for key in list(dict.keys()): > > That creates an entirely new list with a snapshot copy of the keys. If > you then remove elements from the dictionary, the list will still > iterate correctly. The other is to make a set of to_be_deleted keys and delete them all when done. If you only want to delete one key, break the iteration and then delete. > I'm not sure what you're trying to do, but you may find it easier to > use the 'filter' function (which takes an iterable, so possibly use > dict.iteritems() for that).It'll keep some and not others, and then > you can make use of just the ones you get back. -- Terry Jan Reedy