[Tutor] Returns an iterator of tuples
Steven D'Aprano
steve at pearwood.info
Wed Nov 27 15:52:03 CET 2013
On Wed, Nov 27, 2013 at 02:15:22AM -0800, Mike Sila wrote:
> I know what return, iteration and tuples are. Can anyone please tell
> me what an iterator of tuples is?
An iterator is a thing which returns objects one at a time instead of
all at once. The easiest way to get one is to pass a sequence object
like a list, string or tuple to the function iter(). An example:
py> it = iter("Hello!")
py> next(it)
'H'
py> next(it)
'e'
py> next(it)
'l'
py> next(it)
'l'
py> next(it)
'o'
py> next(it)
'!'
py> next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
So an iterator of tuples is an iterator which returns tuples. Here's a
simple example:
py> it = iter([ (1,2), (3,4), (5,6) ])
py> next(it)
(1, 2)
py> next(it)
(3, 4)
py> next(it)
(5, 6)
--
Steven
More information about the Tutor
mailing list