How to iterate the input over a particular size?

joy99 subhakolkata1234 at gmail.com
Mon Dec 28 12:24:08 EST 2009


On Dec 28, 5:28 pm, Vlastimil Brom <vlastimil.b... at gmail.com> wrote:
> 2009/12/27 joy99 <subhakolkata1... 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

vbr,
Just Brilliant.
I manipulated it bit but I got the key, but thanks to all for giving
me lots of solutions and lots of insights over Python. Everytime I
visit this room I learn so much. Thank you all.

Wishing a Great Day ahead,

Best Regards,
Subhabrata.



More information about the Python-list mailing list