Slicing Arrays in this way

Tobiah toby at tobiah.org
Wed May 2 18:29:32 EDT 2007


Matimus wrote:
> 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]]
> 

I had come up with:

[[a[x], a[x + 1]] for x in range(0, 10, 2)]

I was hoping for something a little more concise.
Something like

list[::2:2] if that existed.


-- 
Posted via a free Usenet account from http://www.teranews.com




More information about the Python-list mailing list