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


Groups > comp.lang.python > #77848

Re: Iterator, modify data in loop body

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.069
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'element': 0.07; 'iterate': 0.09; 'ah,': 0.16; 'popping': 0.16; 'sat,': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'looks': 0.24; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; '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; 'received:google.com': 0.35; 'subject:data': 0.36; 'list': 0.37; 'list.': 0.37; 'clear': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'you.': 0.62; "you'll": 0.62; 'more': 0.64; 'welle': 0.84; 'absolutely': 0.87
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=CB//BzPdLeAA+b+/7tLzvKTyRmbV1N7i9GAOX2OkgQE=; b=h2ebxbXjePSDnKeyKzjUA+5SoK1WXmYgTahC/WBaTNbMxKK94lKVhwfx0OYqpsCFLP eaJVK352KqHl9c2jdKgwGcAjzfE9KNc0t8Dfcn+f3yzVm62+qYb/atFLYwqpO59wx2gh 9NUQVgUXjCnE6F3dQpyIbr0carOGDUPloRn+RJE//lbp5Omg1N+/MtADEXtI08218GGR AhRntqA6j8Mm1pV7Giwk6WcxXnBM32BGFkZ+lKcID6lwbVw+WwYNd53cCsgGprxmQqaS y8OjOb7P5A2GtpTJoI3SlWhQ8WwPhT48Dme56Au0xthL9xxSWh8x6XASx/o8kNvUNPQ5 BZuw==
X-Received by 10.68.195.131 with SMTP id ie3mr23526826pbc.145.1410622084036; Sat, 13 Sep 2014 08:28:04 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <qt8debx6im.ln2@news.c0t0d0s0.de>
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>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 13 Sep 2014 09:27:23 -0600
Subject Re: Iterator, modify data in loop body
To 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.13999.1410622093.18130.python-list@python.org> (permalink)
Lines 15
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1410622093 news.xs4all.nl 2912 [2001:888:2000:d::a6]:39683
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77848

Show key headers only | View raw


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.

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