Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70643
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2a.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.070 |
| X-Spam-Evidence | '*H*': 0.86; '*S*': 0.00; 'iterate': 0.09; 'cc:addr :python-list': 0.11; '"to': 0.16; 'dict': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'keys.': 0.16; 'keys:': 0.16; 'option:': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'cc:addr:python.org': 0.22; 'visible': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'header :In-Reply-To:1': 0.27; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'end,': 0.31; 'steven': 0.31; 'subject:all': 0.32; 'are:': 0.33; "i'd": 0.34; 'but': 0.35; 'received:google.com': 0.35; 'two': 0.37; 'performance': 0.37; 'list,': 0.38; 'pm,': 0.38; 'little': 0.38; 'delete': 0.39; 'enough': 0.39; 'deleting': 0.60; 'new': 0.61; "you're": 0.61; 'save': 0.62; 'approaches': 0.68; '"not': 0.84; 'compare:': 0.84; 'contents,': 0.84; 'snapshot': 0.84; 'to:none': 0.92 |
| 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=K1VLZHL1ExHeZls53wbhFzhfLiT7qRBRtK0uLosQTeU=; b=CC01rT25LBSGifm/Y2fKnzmFLgVC/oQIvQfy08oiJmyZec2bFXx8rx1wnxO+bp5v97 Qa6ll4Inji35gR4hescahDMSl8CDVcZoKpoDadOGF72xX4Io8INLwXzjxwxbRe1y9FNs aFakgIhIxc8uLt0AI9pS+IIA3z4WSAla05NAyEaqMe311ZBIp0amkDQlT5tyMY1Yvwip Axk861/IxRmUFQhtYasTXmXq8Lc21zV3EWpVSTQxWZuWUbX0ucp2whVB9qjLqWy+crrS xUdZz6bXCe+W4IJTSJujdX2sahi52k/zvCJYQALWlWiVaZBrfHsND88dAX5NLRSwP9bj Xh2A== |
| MIME-Version | 1.0 |
| X-Received | by 10.58.154.10 with SMTP id vk10mr15738352veb.18.1398566948532; Sat, 26 Apr 2014 19:49:08 -0700 (PDT) |
| In-Reply-To | <535c67e9$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| References | <535AA12A.1030203@earthlink.net> <mailman.9524.1398540343.18130.python-list@python.org> <535c67e9$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| Date | Sun, 27 Apr 2014 12:49:08 +1000 |
| Subject | Re: Proper deletion of selected items during map iteration in for loop: Thanks to all |
| 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.9527.1398566956.18130.python-list@python.org> (permalink) |
| Lines | 26 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1398566956 news.xs4all.nl 2890 [2001:888:2000:d::a6]:45130 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:70643 |
Show key headers only | View raw
On Sun, Apr 27, 2014 at 12:14 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> I think the two obviously good enough approaches are:
>
> - save a "to be deleted" list, then delete those keys;
>
> - copy the "not to be deleted" items into a new dict
For a small enough dict that the performance question doesn't matter,
I'd go with the other option: iterate over a snapshot of the keys.
Compare:
# Naive approach:
for k in d:
if f(k): del d[k]
# Snapshot of keys:
for k in list(d):
if f(k): del d[k]
No extra loop at the end, no switching out and in of contents, just
one little change in the loop header. Obviously you don't want to do
this when you're deleting two out of three billion, but for smallish
dicts, that won't make a visible change.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Proper deletion of selected items during map iteration in for loop: Thanks to all Charles Hixson <charleshixsn@earthlink.net> - 2014-04-26 12:25 -0700
Re: Proper deletion of selected items during map iteration in for loop: Thanks to all Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-27 02:14 +0000
Re: Proper deletion of selected items during map iteration in for loop: Thanks to all Chris Angelico <rosuav@gmail.com> - 2014-04-27 12:49 +1000
Re: Proper deletion of selected items during map iteration in for loop: Thanks to all Duncan Booth <duncan.booth@invalid.invalid> - 2014-04-28 10:48 +0000
Re: Proper deletion of selected items during map iteration in for loop: Thanks to all Roy Smith <roy@panix.com> - 2014-04-26 22:57 -0400
csiph-web