How to iterate the input over a particular size?
vlastimil.brom at gmail.com
Mon Dec 28 13:28:32 CET 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,
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], ]
(possibly using xrange and generator expression instead of the list
More information about the Python-list