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


Groups > comp.lang.python > #110844

Re: Iteration, while loop, and for loop

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Iteration, while loop, and for loop
Date Thu, 30 Jun 2016 07:10:28 -0600
Lines 19
Message-ID <mailman.139.1467292276.2358.python-list@python.org> (permalink)
References <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <577295b3$0$1598$c3e8da3$5496439d@news.astraweb.com> <20160628122308.4578d876@bigbox.christie.dr> <nkudsr$335$1@ger.gmane.org> <CALwzidkBztoyJY=VQu=BuFqVTU3XM0pXa_A+evYOeqGv-6jhbg@mail.gmail.com> <mailman.114.1467214578.2358.python-list@python.org> <577460e2$0$1591$c3e8da3$5496439d@news.astraweb.com> <CALwzidmoUNrAME72wxP0YxhSVuYowennsTHazNqOAm98OSdHJA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de V4trEpPu7Q3QyPOzqEFJlw213Nh4pYxeF3WEwWuya5Qg==
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.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'mutable': 0.09; 'subject:while': 0.09; 'output': 0.13; 'wed,': 0.15; '2016': 0.16; 'iterated': 0.16; 'iterator.': 0.16; 'iterators': 0.16; 'overwriting': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequence.': 0.16; 'wrote:': 0.16; 'char': 0.18; 'prevent': 0.20; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; "d'aprano": 0.33; 'steven': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'but': 0.36; 'needed': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'difference': 0.38; 'no,': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'prompt': 0.79; 'bananas': 0.84; 'implications': 0.84; 'to:name:python': 0.84
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; bh=83FGqMbVtQsGAxG6+UGpyUi2qGA0EPk8IsWEPur69B4=; b=fvQYmyjosk46iGj0b8jJSobigIw8wNZi/w1a6COhzaeKNQx8YWxLngvbwSRoz2HaR1 u/gKHCKL4vGrCA3V5rEo8BhL8uPsvADHihTNdj5Xu+rTjr51u3hyiv5UgFG5dYSVcAgt QIPr2bQw9w8Wsrte7gdizNh/2BobrQsIT7J/175oMBuPr87NTZfJL7UrGwg+sJTD/t2O DWPikyU1jSWLZXxlBc3HTJs6s4gu+O5F0asDGoOHS6xJJ+QfRlq4aIE3kBJwsfYOEvWa JUCFlyGitB6cKiWOomAj2pKmqdDYI21qQncEmutPLJktp7E/LzZYBhs7Q4dgmJ03OIKw Krew==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=83FGqMbVtQsGAxG6+UGpyUi2qGA0EPk8IsWEPur69B4=; b=WEZcZhPmQGErTr5jmps4n41jJyQgzDNlRdnJ38BmyRqiHpGTiNjKMOEalX+Tu8IZ29 xnVz9nOYtjxjAyT5ASUHNeIRwd+p7YNe8DJWwHnIl85sVGCR5QyNp2XbS9aKoyHcY+ST Ro1mG0To1aufHcIXPtbIi28+OwG1ygKzqfYbWuc0iNvf1NHei0CVhLOvQe4l41lK4E9c HzhU13KhxSjn3BVuFyAPiZh4KHEoRxeuPrkcb4AEWm33k0K3P1SNumrsGcPgYCW4Zlrn DGfg661xWJNDkZMYbQyfgF3RNSQgXOvJkTxxOnHy/6fVW3wgDe9yzLrt0h8Rmv7OVqej 2cVQ==
X-Gm-Message-State ALyK8tK9hKkppDxoRgw3Pmq79yWKjWw336U/lulw8KiuIIZNKvlSw0AKUm6vDXGhczIMslHzcDQ/Xw8FgTe7uA==
X-Received by 10.157.48.36 with SMTP id d33mr10002223otc.30.1467292267732; Thu, 30 Jun 2016 06:11:07 -0700 (PDT)
In-Reply-To <577460e2$0$1591$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <CALwzidmoUNrAME72wxP0YxhSVuYowennsTHazNqOAm98OSdHJA@mail.gmail.com>
X-Mailman-Original-References <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <577295b3$0$1598$c3e8da3$5496439d@news.astraweb.com> <20160628122308.4578d876@bigbox.christie.dr> <nkudsr$335$1@ger.gmane.org> <CALwzidkBztoyJY=VQu=BuFqVTU3XM0pXa_A+evYOeqGv-6jhbg@mail.gmail.com> <mailman.114.1467214578.2358.python-list@python.org> <577460e2$0$1591$c3e8da3$5496439d@news.astraweb.com>
Xref csiph.com comp.lang.python:110844

Show key headers only | View raw


On Wed, Jun 29, 2016 at 5:59 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> But there's no need to go to such effort for a mutable iterator. This is
> much simpler:
>
> py> mi = list('bananas')
> py> for char in mi:
> ...     if char == 'a':
> ...             mi.extend(' yum')
> ...     print(char, end='')
> ... else:  # oh no, the feared for...else!
> ...     # needed to prevent the prompt overwriting the output
> ...     print()
> ...
> bananas yum yum yum
> py>

One small difference with this approach is that iterators extended
onto the list are iterated over immediately rather than in sequence.
That could have implications in some scenarios.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Iteration, while loop, and for loop Elizabeth Weiss <cake240@gmail.com> - 2016-06-28 05:36 -0700
  Re: Iteration, while loop, and for loop Michael Selik <michael.selik@gmail.com> - 2016-06-28 13:15 +0000
    Re: Iteration, while loop, and for loop BartC <bc@freeuk.com> - 2016-06-28 14:29 +0100
      Re: Iteration, while loop, and for loop Michael Selik <michael.selik@gmail.com> - 2016-06-28 13:58 +0000
      Re: Iteration, while loop, and for loop Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-28 22:58 -0700
  Re: Iteration, while loop, and for loop BartC <bc@freeuk.com> - 2016-06-28 14:24 +0100
  RE: Iteration, while loop, and for loop "Joseph Lee" <joseph.lee22590@gmail.com> - 2016-06-28 06:26 -0700
  Re: Iteration, while loop, and for loop Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-28 16:42 +0300
  Re: Iteration, while loop, and for loop Michael Selik <michael.selik@gmail.com> - 2016-06-28 13:51 +0000
  Re: Iteration, while loop, and for loop Steven D'Aprano <steve@pearwood.info> - 2016-06-29 01:20 +1000
    Re: Iteration, while loop, and for loop Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-28 18:28 +0300
    Re: Iteration, while loop, and for loop Tim Chase <python.list@tim.thechases.com> - 2016-06-28 12:23 -0500
    Re: Iteration, while loop, and for loop Grant Edwards <grant.b.edwards@gmail.com> - 2016-06-28 17:58 +0000
    Re: Iteration, while loop, and for loop Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-29 09:29 -0600
      Re: Iteration, while loop, and for loop Steven D'Aprano <steve@pearwood.info> - 2016-06-30 09:59 +1000
        Re: Iteration, while loop, and for loop Tim Chase <python.list@tim.thechases.com> - 2016-06-30 05:44 -0500
        Re: Iteration, while loop, and for loop Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-30 07:04 -0600
        Re: Iteration, while loop, and for loop Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-30 07:10 -0600
        Re: Iteration, while loop, and for loop jfong@ms4.hinet.net - 2016-06-30 19:12 -0700
  Re: Iteration, while loop, and for loop Veek M <vek.m1234@gmail.com> - 2016-10-27 14:35 +0530

csiph-web