Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: Iteration, while loop, and for loop Date: Thu, 30 Jun 2016 05:44:59 -0500 Lines: 47 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> <20160630054459.258a0c0a@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de ClcGSkCZiP1KGz0wAlIpFQCv89LHHnvPQbPggbnb/F8g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'versions,': 0.05; 'wrapper': 0.07; 'mutable': 0.09; 'subject:while': 0.09; 'output': 0.13; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterable': 0.16; 'iteration': 0.16; 'iterator.': 0.16; 'overwriting': 0.16; 'queuing': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'set()': 0.16; 'wrote:': 0.16; 'char': 0.18; 'prevent': 0.20; 'fairly': 0.22; '"",': 0.22; 'consistent': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'said,': 0.27; 'went': 0.28; 'container': 0.29; 'types.': 0.29; 'there.': 0.30; 'code': 0.30; 'certain': 0.31; 'changed': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'know.': 0.34; 'file': 0.34; 'but': 0.36; 'needed': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'charset:us-ascii': 0.37; 'no,': 0.38; '(1)': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'back': 0.62; 'needs,': 0.63; 'oldest': 0.66; 'prompt': 0.79; 'bananas': 0.84; 'dict()': 0.84; 'received:23': 0.84; 'have.': 0.93 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1467283501543:3152473647 X-MC-Ingress-Time: 1467283501543 In-Reply-To: <577460e2$0$1591$c3e8da3$5496439d@news.astraweb.com> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.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: <20160630054459.258a0c0a@bigbox.christie.dr> 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:110839 On 2016-06-30 09:59, 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> > > > This example shows two things: > > (1) There's no need for a MutableIterator, we have list; Convenient to know. I was fairly certain that this had failed for me in past versions, but I went back to the oldest I have (2.4) and it still works there. Might have to revisit some queuing code I have. That said, it's not consistent across iterable container types. With mi = set('bananas') for char in mi: if char == 'a': mi.add('X') print(char) else: print() I get Traceback (most recent call last): File "", line 1, in RuntimeError: Set changed size during iteration If the list() meets your needs, then you're in luck. But just as frequently, I want to use a set() or a dict() and have to write my own wrapper around it. -tkc