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


Groups > comp.lang.python > #70609

Re: Proper deletion of selected items during map iteration in for loop

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'explicitly': 0.05; 'duplicate': 0.07; 'cc:addr:python-list': 0.11; 'assume': 0.14; '(k,': 0.16; 'charles': 0.16; 'dict': 0.16; 'dictionary,': 0.16; 'expected):': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration': 0.16; 'keys:': 0.16; 'loop.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'work,': 0.20; 'example': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'list:': 0.30; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'code': 0.31; 'keys': 0.31; 'way?': 0.31; 'cases': 0.33; 'third': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'filter': 0.38; 'list,': 0.38; 'previous': 0.38; 'does': 0.39; 'delete': 0.39; 'changed': 0.39; 'easy': 0.60; 'entire': 0.61; 'simply': 0.61; 'save': 0.62; 'real': 0.63; 'more': 0.64; 'effectively': 0.66; '26,': 0.68; 'snapshot': 0.84; 'to:none': 0.92; 'technique': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=NUpg8g9bRqN51bxtT2QlHTgDp/98lP4YBLT+bW6I/8Y=; b=p6S7ID6T9U2qhzp6DrBQUVJQ/EKrl+tVKhbSslExkX9N9GONBB8kl7GsnTHLYn5Jjp 3nprMRFmubBfpMNFrF4cr/i8YEjeR+mKjpY8AzpRXF0z+JIv3tKobtXkQAqcBDEzRx0d mfFeJ1ZNmgy7AlQvUfmx3GRSFzq4g23pC0PVpxg7TEY/mP8i5fcMfPT6CRGfHpjfc/ww /vw2Uh1eF9xoAeKv0fXLbsf0iCKCgTGe5BPtr3lv+CYWzJpRAgwKeahJk4iwc/iZxj/Z HVEsNLsTzXAejHvYQx/9244GDDO+OLln3ebONnYikL7H80SRPyTXRRxqR/UygV77po39 JGOA==
MIME-Version 1.0
X-Received by 10.58.74.38 with SMTP id q6mr7586089vev.7.1398448853290; Fri, 25 Apr 2014 11:00:53 -0700 (PDT)
In-Reply-To <535AA12A.1030203@earthlink.net>
References <535AA12A.1030203@earthlink.net>
Date Sat, 26 Apr 2014 04:00:53 +1000
Subject Re: Proper deletion of selected items during map iteration in for loop
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9505.1398448862.18130.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398448862 news.xs4all.nl 2939 [2001:888:2000:d::a6]:50623
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70609

Show key headers only | View raw


On Sat, Apr 26, 2014 at 3:53 AM, Charles Hixson
<charleshixsn@earthlink.net> 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?

One easy way is to explicitly coalesce the items() view into a list:

for k, v in list(m.items()):

That would work, but you effectively duplicate your entire dictionary
into a list. More likely, you should simply snapshot the keys:

for k in list(m):
    # Your example code isn't using v, but I
    # assume the real code does
    v = m[k]
    if f(k):
        # as above
        del m[k]

This and your previous technique of accumulating a delete-me list
would be the two standard ways to filter a dictionary with a loop. (I
say "with a loop" because there's a third way to filter a dictionary,
and that's with a comprehension. But I don't know of a really clean
way to save v elsewhere for the cases where the dict isn't keeping
that key.)

ChrisA

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


Thread

Re: Proper deletion of selected items during map iteration in for loop Chris Angelico <rosuav@gmail.com> - 2014-04-26 04:00 +1000

csiph-web