a sequence question

F. Petitjean littlejohn.75 at news.free.fr
Fri Jan 28 10:03:00 EST 2005


Le Fri, 28 Jan 2005 13:59:45 GMT, Chris Wright a écrit :
> Hi,
> 
> 1) I want to iterate over a list "N at a time"
> 
> 
> Is there a nifty way to do with with list comprehensions,
> or do I just have to loop over the list ?
> 
> cheers and thanks
seq = xrange(1, 9)  # an iterable  [1, 2, ... 8]
N = 2
it = (iter(seq,)*N  # a tuple containing N times the *same* iterator on
seq
print zip(*it)   # the list you are after
from itertools import izip
help(izip)
it = (iter(seq),)*2
for tup in izip(*it):
    print tup
> 
> chris wright



More information about the Python-list mailing list