Listing 4. Iteration Example
>>> langs = ['c', 'perl', 'tcl/tk']
>>> langIter =iter(langs)
>>> langs.append('c++')
>>> langIter.next()
'c'
>>> langIter.next()
'perl'
>>> langIter.next()
'tcl/tk'
>>> langs
['c', 'perl', 'tcl/tk', 'c++']
>>> langIter.next() # added after
# iter obj created
'c++'
>>> langs.remove('perl')
>>> langs.append('ruby')
>>> langs
['c', 'tcl/tk', 'c++', 'ruby']
>>> langIter.next() # out of elements
Traceback (most recent call last):
File "", line 1, in ?
langIter.next()
StopIteration
>>> langs.append('java') # added item
# to tail
>>> langIter.next() # resume iteration
'java'
Copyright © 1994 - 2019 Linux Journal. All rights reserved.