breaking up a list

Larry Bates lbates at syscononline.com
Tue Sep 28 09:19:30 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.
> 
> Many thanks
> 
> Colin
> 
> 
I would use:

pairs=[[a,b] for a,b in x]

but as you can see from other replies there are other ways.
Note: this wouldn't work if there were not an EVEN number
of data items in the list.

Larry Bates
Syscon, Inc.



More information about the Python-list mailing list