Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77849
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.040 |
| X-Spam-Evidence | '*H*': 0.92; '*S*': 0.00; 'element': 0.07; 'iterate': 0.09; 'repeated': 0.09; 'cc:addr:python-list': 0.11; 'ah,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration.': 0.16; 'popping': 0.16; 'sat,': 0.16; 'all.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'looks': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'michael': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; '13,': 0.31; 'sep': 0.31; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'subject:data': 0.36; 'list': 0.37; 'list.': 0.37; 'clear': 0.37; 'thank': 0.38; 'list,': 0.38; 'rather': 0.38; 'that,': 0.38; 'sure': 0.39; 'either': 0.39; 'ian': 0.60; 'new': 0.61; 'you.': 0.62; "you'll": 0.62; 'more': 0.64; 'welle': 0.84; 'absolutely': 0.87; 'inefficient': 0.91; '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=4RmDswzMXEa3QmYbWDZOoYuoZ5wl5kXUlMsIKmlZN5g=; b=JMto3jsV69dgMvJ9AdGwBuRRilkWXxbR9v8V4HyGJWoV252TLv24xtxgOQCOjdT+/M p4JeY+WVZYfYI+TQAzxZYjdk5ASZnk5zR+LMUnFGi2fkrU/IyHMjxqeeZvoOfuyeq1Jj GxtJ+MrxVaMjl+sunjGfVVqqnXFwenohqDS5Kxlr6ecNlmy2kTOCvuGFshQBChimFxvN gmejoQsVdxt+CBOxnr6u/R+wmnMAVXv/zo6RZ6C/DxQap4A5UsaJ+FpbOlhy6lKY4X/t zl9wg7mQjAzR+rpKuh74CQbvNdfu9fCkhG8OKg62YT13I1nHF8MA1XEk/DQk2foD7E+r Pjcw== |
| MIME-Version | 1.0 |
| X-Received | by 10.50.30.72 with SMTP id q8mr10547213igh.14.1410622368251; Sat, 13 Sep 2014 08:32:48 -0700 (PDT) |
| In-Reply-To | <CALwzid=ukvNr2GpLB+cFDqGykf9rtHVkxG_LQTtxrAt_7nBJ8Q@mail.gmail.com> |
| References | <uk3debxpcg.ln2@news.c0t0d0s0.de> <mailman.13990.1410589338.18130.python-list@python.org> <4o6debxojk.ln2@news.c0t0d0s0.de> <mailman.13992.1410592983.18130.python-list@python.org> <qt8debx6im.ln2@news.c0t0d0s0.de> <CALwzid=ukvNr2GpLB+cFDqGykf9rtHVkxG_LQTtxrAt_7nBJ8Q@mail.gmail.com> |
| Date | Sun, 14 Sep 2014 01:32:48 +1000 |
| Subject | Re: Iterator, modify data in loop body |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | Python <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.14000.1410622376.18130.python-list@python.org> (permalink) |
| Lines | 29 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1410622376 news.xs4all.nl 2959 [2001:888:2000:d::a6]:44185 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:77849 |
Show key headers only | View raw
On Sun, Sep 14, 2014 at 1:27 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Sat, Sep 13, 2014 at 1:39 AM, Michael Welle <mwe012008@gmx.net> wrote:
>>> In that case, don't iterate over the list at all. Do something like this:
>>>
>>> while lst:
>>> element = lst.pop(0)
>>> # work with element
>>> lst.append(new_element)
>>>
>>> There's no mutation-while-iterating here, and it's clear that you'll
>>> keep going until there's absolutely nothing left.
>> Ah, that looks like a good approach, thank you.
>
> Also note that this approach (appending to the end and popping from
> the front) will be more efficient using a collections.deque than a
> list.
Sure it will - that's kinda the point of a double-ended queue, to
avoid all the inefficient movements :) But the concept is still the
same: do repeated mutations rather than iteration. Either that, or
iterate and build up a new list, either with a comprehension or with
something like this:
newlst = []
for element in lst:
# work with element
newlst.append(new_element)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 08:09 +0200
Re: Iterator, modify data in loop body Chris Angelico <rosuav@gmail.com> - 2014-09-13 16:22 +1000
Re: Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 09:01 +0200
Re: Iterator, modify data in loop body Chris Angelico <rosuav@gmail.com> - 2014-09-13 17:22 +1000
Re: Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 09:39 +0200
Re: Iterator, modify data in loop body Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-13 09:27 -0600
Re: Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 18:58 +0200
Re: Iterator, modify data in loop body Chris Angelico <rosuav@gmail.com> - 2014-09-14 01:32 +1000
Re: Iterator, modify data in loop body Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2014-09-16 10:49 +0200
Re: Iterator, modify data in loop body Chris Angelico <rosuav@gmail.com> - 2014-09-17 01:19 +1000
Re: Iterator, modify data in loop body Peter Otten <__peter__@web.de> - 2014-09-13 09:34 +0200
Re: Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 09:55 +0200
csiph-web