Iteration, while loop, and for loop
ian.g.kelly at gmail.com
Thu Jun 30 09:10:28 EDT 2016
On Wed, Jun 29, 2016 at 5:59 PM, Steven D'Aprano <steve at 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
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.
More information about the Python-list