Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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.036 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'element': 0.07; 'append': 0.09; 'iterate': 0.09; 'cc:addr:python-list': 0.11; 'deque': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'sure.': 0.16; 'think.': 0.16; 'elements': 0.16; 'sat,': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'possible,': 0.19; 'help.': 0.21; 'cc:addr:python.org': 0.22; 'example.': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'michael': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'posting': 0.31; '13,': 0.31; 'sep': 0.31; 'minimal': 0.33; 'something': 0.35; 'case,': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:data': 0.36; 'application': 0.37; 'list': 0.37; 'clear': 0.37; 'depends': 0.38; 'pm,': 0.38; 'algorithms': 0.60; "you'll": 0.62; 'real': 0.63; 'different': 0.65; 'natural': 0.68; 'front.': 0.84; 'welle': 0.84; 'absolutely': 0.87; 'cutting': 0.91; 'involved.': 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=lpgtJqnQFeY5NGrTUrE4EVj5mw3a2o7cEDgcq6Xlm0M=; b=YRDPU4plz4DfXS0UQJxthj15fcr27tlGY70j+fzwXMo964ERw+HZv/wt9gzDx3q+xi VJX/lLAIk5fSOklsuzSAjBw5Z/cwMj82Umpi0wkdQ0G1iehC1rIi48O3NxEKxz8yVUrn sAnxpaQajrrxC274meIlkhlWCDlm3PeokR5kn7XMoSDvCGJmoN+ginEqAh9Yuil8/hyr me0uZ7I5omdyWL6jqODhBYC2yepaxzVls3hZ2X+lxjqraScZsJRfbeFrP8K34jNqCPWU k0vWklxK6jj+Jbc9YZtgG4Slna9r3eo5Z9eoW7fPBoGS4tivgwH1qIMLg3x2WE5MhW58 HwXQ== MIME-Version: 1.0 X-Received: by 10.50.66.197 with SMTP id h5mr8147445igt.34.1410592974531; Sat, 13 Sep 2014 00:22:54 -0700 (PDT) In-Reply-To: <4o6debxojk.ln2@news.c0t0d0s0.de> References: <4o6debxojk.ln2@news.c0t0d0s0.de> Date: Sat, 13 Sep 2014 17:22:54 +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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1410592983 news.xs4all.nl 2861 [2001:888:2000:d::a6]:41213 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77836 On Sat, Sep 13, 2014 at 5:01 PM, Michael Welle 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. :) > 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. ChrisA