Slicing Arrays in this way

James Stroud jstroud at mbi.ucla.edu
Wed May 2 21:25:56 EDT 2007


Tobiah wrote:
> 
>  >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])
> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
> 
> 

Here's one I use:

def elegant_solution(alist):
   i = iter(alist)
   return [[j, i.next()] for j in i]


py> elegant_solution(range(14))
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13]]

James



More information about the Python-list mailing list