list to list of pairs

Steve Holden sholden at holdenweb.com
Mon Nov 19 19:39:21 CET 2001


<bvdpoel at uniserve.com> wrote ...
> 
> I've sure I've seen this somewhere...but I can't today...
> 
> I need to convert a list like [1,2,3,4,5,6] to a list of pairs [[1,2],
> [3,4], [5,6]]. Isn't there a way to do this with map() or zip()???
> 
>>> def pairs(l):
...  return [[l[i], l[i+1]] for i in range(0, len(l), 2)]
... 
>>> pairs([1,2,3,4,5,6])
[[1, 2], [3, 4], [5, 6]]
>>> 

regards
 Steve
---
http://www.holdenweb.com/








More information about the Python-list mailing list