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: 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: References: <4o6debxojk.ln2@news.c0t0d0s0.de> From: Ian Kelly Date: Sat, 13 Sep 2014 09:27:23 -0600 Subject: Re: Iterator, modify data in loop body To: 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: 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 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.