breaking up a list

Curzio Basso curzio.basso at unibas.ch
Tue Sep 28 07:41:19 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.

don't know if it's the best way but you can try this:

pairs = [x[2*i:2*i+2] for i in xrange(len(x)/2)]



More information about the Python-list mailing list