breaking up a list

Peter Hansen peter at engcorp.com
Tue Sep 28 07:19:16 EDT 2004


C Gillespie wrote:
> Dear All,
> 
> If I have a list, say
> 
> x=[1,2,3,4,5,6]
> 
> What's the best way of converting it into this: [[1, 2], [3, 4], [5, 6]],
> i.e. splitting it into pairs.

There are a variety of solutions in the Python Cookbook
here http://aspn.activestate.com/ASPN/Cookbook/Python?kwd=Algorithms

I notice "Getting items in batches", and "Group a list into
sequential n-tuples" right off the bat.

-Peter



More information about the Python-list mailing list