Slicing Arrays in this way

Matimus mccredie at gmail.com
Wed May 2 18:17:45 EDT 2007


On May 2, 3:03 pm, Tobiah <t... at tobiah.org> wrote:
>  >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])
> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com

>>> seq = range(1,11)
>>> seq
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> zip( seq[0::2],seq[1::2] )
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

if you _really_ need lists then...
>>> map(list, zip( seq[0::2],seq[1::2] ))
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]




More information about the Python-list mailing list