Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly 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: References: <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <577295b3$0$1598$c3e8da3$5496439d@news.astraweb.com> <20160628122308.4578d876@bigbox.christie.dr> <577460e2$0$1591$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de V4trEpPu7Q3QyPOzqEFJlw213Nh4pYxeF3WEwWuya5Qg== 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; '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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <577295b3$0$1598$c3e8da3$5496439d@news.astraweb.com> <20160628122308.4578d876@bigbox.christie.dr> <577460e2$0$1591$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:110844 On Wed, Jun 29, 2016 at 5:59 PM, Steven D'Aprano 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.