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


Groups > comp.lang.python > #110839

Re: Iteration, while loop, and for loop

From Tim Chase <python.list@tim.thechases.com>
Newsgroups comp.lang.python
Subject Re: Iteration, while loop, and for loop
Date 2016-06-30 05:44 -0500
Message-ID <mailman.136.1467288200.2358.python-list@python.org> (permalink)
References (3 earlier) <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> <20160630054459.258a0c0a@bigbox.christie.dr>

Show all headers | View raw


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 "<stdin>", line 1, in <module>
  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

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