Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70616
| 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 | <python.list@tim.thechases.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.018 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; 'else:': 0.03; 'duplicate': 0.07; 'rest,': 0.07; 'iterate': 0.09; 'mentions': 0.09; 'mostly': 0.14; '(k,': 0.16; '-tkc': 0.16; 'above)': 0.16; 'clone': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'keep,': 0.16; 'optionally': 0.16; 'reedy': 0.16; 'storing': 0.16; 'subject: \n ': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'items.': 0.19; 'options': 0.25; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'chris': 0.29; 'code': 0.31; 'keys': 0.31; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'done': 0.36; 'charset:us- ascii': 0.36; 'should': 0.36; 'half': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'expect': 0.39; 'volume': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'deleting': 0.60; 'free': 0.61; 'entire': 0.61; 'back': 0.62; 'save': 0.62; 'such': 0.63; 'refer': 0.63; 'more': 0.64; 'kept': 0.65; 'between': 0.67; '*and*': 0.84; 'describes': 0.84; 'dict,': 0.84; 'received:50.22': 0.84; 'items,': 0.91 |
| Date | Fri, 25 Apr 2014 14:22:59 -0500 |
| From | Tim Chase <python.list@tim.thechases.com> |
| To | python-list@python.org |
| Subject | Re: Proper deletion of selected items during map iteration in for loop |
| In-Reply-To | <ljearh$qde$1@ger.gmane.org> |
| References | <535AA12A.1030203@earthlink.net> <535AA3B8.3080308@mrabarnett.plus.com> <ljearh$qde$1@ger.gmane.org> |
| X-Mailer | Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| X-AntiAbuse | This header was added to track abuse, please include it with any abuse report |
| X-AntiAbuse | Primary Hostname - boston.accountservergroup.com |
| X-AntiAbuse | Original Domain - python.org |
| X-AntiAbuse | Originator/Caller UID/GID - [47 12] / [47 12] |
| X-AntiAbuse | Sender Address Domain - tim.thechases.com |
| X-Get-Message-Sender-Via | boston.accountservergroup.com: authenticated_id: tim@thechases.com |
| 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.9512.1398453800.18130.python-list@python.org> (permalink) |
| Lines | 44 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1398453800 news.xs4all.nl 2852 [2001:888:2000:d::a6]:49434 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:70616 |
Show key headers only | View raw
On 2014-04-25 14:50, Terry Reedy wrote:
> If you expect to delete more than half the keys *and* if there are
> no other references to the dict, such that you need the particular
> object mutated, this might be better.
If that's your precondition, then it might be better to do something
like
keep = {}
for (k, v) in m.items():
if f(k):
# do some processing of v and save result elsewhere
else:
keep[k] = v
m.clear()
m.update(keep)
del keep # optionally free this back up when done
This would allow things that refer to "m" to retain the same
reference, but should have the same result as deleting them without
having to duplicate what the OP describes as a large volume of data.
Either way, the options are mostly
1) clone the entire .items() into a list, and iterate over that for
deleting from your underlying view (what Chris & Matthew suggest)
2) iterate over the items, storing up the ones to delete and delete
them all at the end (what the OP mentions having tried before)
3) iterate over the items, storing up the ones to keep, delete the
rest, and then put the kept ones back (what I code above)
The choice between #2 and #3 hinge on whether you expect to delete
more than half the items. If you plan to delete more than half, store
the ones to keep (#3); if you plan to delete less than half, store
the ones to delete (#2).
-tkc
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Proper deletion of selected items during map iteration in for loop Tim Chase <python.list@tim.thechases.com> - 2014-04-25 14:22 -0500
csiph-web