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: 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: References: <4o6debxojk.ln2@news.c0t0d0s0.de> Date: Sun, 14 Sep 2014 01:32:48 +1000 Subject: Re: Iterator, modify data in loop body From: Chris Angelico Cc: Python 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: 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 On Sun, Sep 14, 2014 at 1:27 AM, Ian Kelly wrote: > On Sat, Sep 13, 2014 at 1:39 AM, Michael Welle 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