Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '16,': 0.03; 'element': 0.07; 'iterate': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'element.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'marker': 0.16; 'popping': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'chris': 0.29; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'sep': 0.31; 'another': 0.32; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'subject:data': 0.36; 'yield': 0.36; 'next': 0.36; 'list': 0.37; 'level': 0.37; 'pm,': 0.38; 'that,': 0.38; 'expect': 0.39; 'simply': 0.61; 'advanced': 0.63; 'thomas': 0.65; 'rachel': 0.84; 'thing,': 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=71WWigobLW+/IK2/ns9mPeRnmP8hIpWDMfDEgN3lN2c=; b=N11SEeziQP+POm5fQyepBFcg3IwX3eEXuTCOylBRTJF33+gf6oQewkClRa3ZuZFblj a7X+JDTpifel4y+LavTOUsJ2dSVWer7glNPxytgZHUcqi7hedlr7aQMXoXjy4Y69QVm/ kaNZvNqRIv2nmvCSflUmoIQtFJHX40tirUiwN2F60zoPMupW9TpiQfm0rBnNdWOpZfmt QPHelvVk4ayGYBQPODcn6YNLzFDHIvUgqfih7bb7kl90Vw+IJHNLxE6uT/hvbC18Y/sC cKGMqMUas2o0gHxZi3IJr7XuqxOiXLpr+qaiRhtoLhZn+8J8CKwjdp0w867ZOpHaloz3 z1Ow== MIME-Version: 1.0 X-Received: by 10.50.20.169 with SMTP id o9mr3010716ige.14.1410880768390; Tue, 16 Sep 2014 08:19:28 -0700 (PDT) In-Reply-To: References: <4o6debxojk.ln2@news.c0t0d0s0.de> Date: Wed, 17 Sep 2014 01:19:28 +1000 Subject: Re: Iterator, modify data in loop body From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1410880778 news.xs4all.nl 2958 [2001:888:2000:d::a6]:34329 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77937 On Tue, Sep 16, 2014 at 6:49 PM, Thomas Rachel wrote: > Am 13.09.2014 09:22 schrieb Chris Angelico: > >> 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) > > > And if you don't like that, define a > > def iter_pop(lst): > while lst: > yield lst.pop(0) > > and you can do > > for element in iter_pop(lst): But that's exactly the same thing, with another level of indirection. It certainly isn't the advantage you'd expect from an iterator, namely that it simply stores a marker that gets advanced to the next element. Popping the 0th element is costly, wrapping it into an iterator conceals that. ChrisA