array next pointer

Duncan Booth duncan.booth at invalid.invalid
Wed Mar 18 09:49:32 CET 2009

Armin <feng.shaun at> wrote:

> Could you give an example of next() with a sentinel and describe its
> use case please?  I have a little trouble understanding what you guys
> mean! 

It means you don't have to worry about next() throwing StopIteration. 

>>> def pairs(sequence, padding=None):
	sequence = iter(sequence)
	for a in sequence:
		b = next(sequence, padding)
		yield a, b

>>> list(pairs('abcd'))
[('a', 'b'), ('c', 'd')]
>>> list(pairs('abcde'))
[('a', 'b'), ('c', 'd'), ('e', None)]

Duncan Booth

More information about the Python-list mailing list