Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'formed': 0.07; 'subject:changing': 0.07; 'dict': 0.09; 'exception.': 0.09; 'iterate': 0.09; 'am,': 0.14; 'received:209.85.214.174': 0.14; 'received:mail-iw0-f174.google.com': 0.14; 'wrote:': 0.14; 'angelico': 0.16; 'dictionary,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterable,': 0.16; 'keys.': 0.16; 'possibly': 0.16; 'mon,': 0.17; '(which': 0.20; 'header:In- Reply-To:1': 0.21; 'trying': 0.23; '\xa0if': 0.23; 'correctly.': 0.25; 'do,': 0.25; 'function': 0.25; "i'm": 0.27; 'message- id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'elements': 0.29; 'explicitly': 0.29; 'subject:How': 0.30; 'iterating': 0.30; 'to:addr:python-list': 0.33; 'list': 0.33; 'chris': 0.34; 'takes': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; '20,': 0.37; 'but': 0.38; 'back.': 0.38; 'creates': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'easier': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'entirely': 0.40; 'pop': 0.65; 'cause': 0.67; '12:32': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=7FWI9MG4xpmQuZScmo0njlEhW7xypbr2WqXTNXfJeBU=; b=O3oI5mFY2vaNP/7NpL7bnnoXJwsSwsMf5r1mjyHhSNld6tSqD1h8fIlCEmXyNqYEP8 8CnDdd84wg9leNiRRLDPlZd6kgKz/blRi++P4+jrXSIQnwP9RrqXij4ur+yVStbcYCzo vjvPBLfaznT/tz1fZRSNSXmSKzraEOGOhjNC8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=pmiVUWiRDQZBPWzDLwj0eF2o6qPmVEBSN3bIMJE8mU3jL7jPY7qQJ1bdihit1LdOS4 zZ3ab/l1xdN/GOh5T24Ht324lMbIawH8zdCu4Em6uEWHIC4YWhTwP85WGjOt/nA+2wKM ib7dkDQxOHzm03JQWTw7mOYWXfEsIWnC4JEvU= MIME-Version: 1.0 In-Reply-To: References: Date: Mon, 20 Jun 2011 01:13:58 +1000 Subject: Re: How to iterate on a changing dictionary From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 32 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308496441 news.xs4all.nl 49178 [::ffff:82.94.164.166]:46559 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7969 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): > =A0 for key in dict.keys(): > =A0 =A0 =A0if dict[key] is not my_result: > =A0 =A0 =A0 =A0 dict.pop(key) > =A0 =A0else: > =A0 =A0 =A0 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. 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. Chris Angelico