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


Groups > comp.lang.python > #77838

Re: Iterator, modify data in loop body

From Michael Welle <mwe012008@gmx.net>
Newsgroups comp.lang.python
Subject Re: Iterator, modify data in loop body
Date 2014-09-13 09:39 +0200
Organization MB-NET.NET for Open-News-Network e.V.
Message-ID <qt8debx6im.ln2@news.c0t0d0s0.de> (permalink)
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>

Show all headers | View raw


Hello,

Chris Angelico <rosuav@gmail.com> writes:

> On Sat, Sep 13, 2014 at 5:01 PM, Michael Welle <mwe012008@gmx.net> wrote:
>> ideed, this works for the minimal example. In a real application list
>> comprehension might be a bit unhandy, because there is a lot of code
>> involved.
>
> Sure. Sometimes, cutting something down for posting makes a completely
> different solution possible, and that doesn't much help. :)
and sometimes new and interesting ideas come up doing so ;).


>> It depends on the use case I think. For some algorithms it feels natural
>> to just append at the end of the list while consuming elements from the
>> front. I think a deque supports that as well.
>
> 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.

Regards
hmw

-- 
biff4emacsen - A biff-like tool for (X)Emacs
http://www.c0t0d0s0.de/biff4emacsen/biff4emacsen.html
Flood - Your friendly network packet generator
http://www.c0t0d0s0.de/flood/flood.html

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


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