Transforming a List of elements into a List of lists of elements

tiago almeida tiago.b.almeida at gmail.com
Fri Jan 8 06:39:06 EST 2010


Hello all,

I'd like to ask how you'd implement a function *f* that transforms a list of
elements into a list of lists of elements by grouping contiguous elements
together.
Examples:

a=[1,2,3,4,5]
print( f(a, 2) )   # -> [ [1, 2], [3, 4], [5] ]
print( f(a, 3) )   # -> [ [1, 2, 3], [4, 5] ]
print( f(a, 1) )   # -> [ [1], [2], [3], [4], [5] ]


I have done a small function that does this using a while loop but what I'm
looking for is a more *pythonic* way to do this. Maybe some function in some
standard Module does this and I don't know?

Ps: I don't post the function here because I don't have it with me.

Thanks a lot in advance!
Tiago Almeida
tiago.b.almeida at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100108/c451124b/attachment.html>


More information about the Python-list mailing list