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


Groups > comp.lang.python > #70607

Proper deletion of selected items during map iteration in for loop

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <charleshixsn@earthlink.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.047
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; '(k,': 0.16; 'charles': 0.16; 'expected):': 0.16; 'iteration': 0.16; 'message- id:@earthlink.net': 0.16; 'received:dsl.mindspring.com': 0.16; 'separate': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; 'gives': 0.31; 'keys': 0.31; 'way?': 0.31; 'received:66': 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; 'save': 0.62
Date Fri, 25 Apr 2014 10:53:46 -0700
From Charles Hixson <charleshixsn@earthlink.net>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Proper deletion of selected items during map iteration in for loop
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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.9503.1398448502.18130.python-list@python.org> (permalink)
Lines 17
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398448502 news.xs4all.nl 2859 [2001:888:2000:d::a6]:43990
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70607

Show key headers only | View raw


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?

-- 
Charles Hixson

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


Thread

Proper deletion of selected items during map iteration in for loop Charles Hixson <charleshixsn@earthlink.net> - 2014-04-25 10:53 -0700

csiph-web