for loop question
Thu Oct 11 10:24:50 CEST 2007
Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> >> >>> pairs = (test[i:i+2] for i in xrange(len(test)-1))
> >> >>> for a,b in pairs:
> >> ... print a,b
> > for a, b in zip(test, test[1:]):
> > print a, b
> May be unfortunately slow if test is half a gigabyte of data, what with
> essentially making three copies of it. Lazy procedures like the generator
> expression above are often much better.
from itertools import izip, islice
for a,b in izip(test, islice(test,1,None)):
seems more natural.
More information about the Python-list