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


Groups > comp.lang.python > #7969

Re: How to iterate on a changing dictionary

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 <rosuav@gmail.com>
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 <itl1a5$rom$1@speranza.aioe.org>
References <itl1a5$rom$1@speranza.aioe.org>
Date Mon, 20 Jun 2011 01:13:58 +1000
Subject Re: How to iterate on a changing dictionary
From Chris Angelico <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.154.1308496441.1164.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Mon, Jun 20, 2011 at 12:32 AM, TheSaint <nobody@nowhere.net.no> 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.

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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-19 22:32 +0800
  Re: How to iterate on a changing dictionary Chris Angelico <rosuav@gmail.com> - 2011-06-20 01:13 +1000
    Re: How to iterate on a changing dictionary Roy Smith <roy@panix.com> - 2011-06-19 11:53 -0400
      Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-19 12:51 -0400
  Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-19 12:02 -0400
  Re: How to iterate on a changing dictionary Lie Ryan <lie.1296@gmail.com> - 2011-06-20 03:00 +1000
    Re: How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-20 21:20 +0800
      Re: How to iterate on a changing dictionary Florencio Cano <florencio.cano@gmail.com> - 2011-06-20 16:30 +0200
      Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-20 13:37 -0400
        Re: How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-21 18:44 +0800

csiph-web