On Wed, Jun 29, 2016 at 5:59 PM, Steven D'Aprano <st...@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. -- https://mail.python.org/mailman/listinfo/python-list