How to iterate the input over a particular size?

Vlastimil Brom vlastimil.brom at gmail.com
Mon Dec 28 07:28:32 EST 2009


2009/12/27 joy99 <subhakolkata1234 at gmail.com>:

> Dear Group,
> Answers were good. But I am looking for a smarter solution like:
>
> for i[:2] in list:
> ....
>
> etc. or by doing some looping over loop.
> Do not worry I'll work out the answer.
>
> Wishing you a happy day ahead,
> Regards,
> Subhabrata.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Hi,
maybe just something like:

>>> input_list = range(10)
>>> n = 3
>>> [input_list[i:i+n] for i in range(0, len(input_list), n)]
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
>>>
?
(possibly using xrange and generator expression instead of the list
comprehension)

 vbr



More information about the Python-list mailing list