Objective
|
Lesson
Iteration is the process of moving from one member of a sequence to the next. Depending on the code that accesses the sequence, the member currently accessed may be retrieved, changed or ignored. For iteration to be possible, the sequence or object must be "iterable." Python's error messages tell us whether or not an object is iterable: >>> 6 in 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'int' is not iterable
>>>
>>> 6 in [1] ; 6 in [4,5,6,7]
False
True
>>>
Use built-in function >>> next([1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not an iterator
>>>
A Iteration is important because the concept is so elementary that it is almost impossible to write meaningful code without it.
|
Iterables
Many common and familiar sequences are iterables. Python's >>> tuple(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>
>>> tuple([1,2,3]) # list is iterable.
(1, 2, 3)
>>> tuple((1,2,3)) # tuple is iterable.
(1, 2, 3)
>>> tuple( {1,2,3,2,3,3} ) # set is iterable.
(1, 2, 3)
>>> tuple( '123' ) # string is iterable.
('1', '2', '3')
>>> tuple( bytes([1,2,3]) ) # bytes object is iterable.
(1, 2, 3)
>>> tuple( bytearray([1,2,3]) ) # bytearray is iterable.
(1, 2, 3)
>>> tuple ( range(2,10,3) ) # range(...) built-in is iterable.
(2, 5, 8)
>>> tuple ( {'one': 1, 'two': 2, 'three': 3} ) # dictionary is iterable.
('one', 'two', 'three')
>>>
When we say "dictionary is iterable," iteration over a dictionary means iteration over the keys of the dictionary. When an object is iterable, we expect it to support operations over iterables: >>> 2 in {1,2,3}
True
>>> 'abc' in ' abcd '
True
>>> for p in (2,3,4) : print (p)
...
2
3
4
>>> 2 in {'one': 1, 'two': 2, 'three': 3}
False
>>> 'two' in {'one': 1, 'two': 2, 'three': 3}
True
>>> [ {'one': 1, 'two': 2, 'three': 3}[p] for p in {'one': 1, 'two': 2, 'three': 3} ]
[1, 2, 3] # Retrieving the values of the dictionary.
>>>
If you know exactly how many items the iterable contains, the following syntax is possible: >>> d,e = {1,2} ; d ; e
1
2
>>>
>>> v1,v2,v3 = {'one': 1, 'two': 2, 'three': 3} ; v1 ; v2 ; v3
'one'
'two'
'three'
>>>
If the iterable contains one member: >>> L1 = [6] ; L1
[6]
>>> L1, = [6] ; L1
6
>>>
|
Iterators
If interrupted, iteration over an iterable does not resume at the point of interruption. If iteration is interrupted and resumed, the next iteration returns to the beginning. >>> for p in 'abcd' :
... print(p)
... if p == 'b' : break
...
a
b
>>> for p in 'abcd' :
... print(p)
...
a
b
c
d
>>>
Iterators within Python allow for interruption of the iteration and resumption at the point immediately after that at which interruption occurred. Function
|
Assignments
Enhanced
|
Further Reading or Review
References
1. Python's documentation: "Iterators," "Data Types That Support Iterators," "Generator expressions and list comprehensions," "Generators"
"iter(object[, sentinel])," "next(iterator[, default])," "tuple([iterable])," " range(start, stop[, step])" |