[Tutor] Every Other

jfouhy@paradise.net.nz jfouhy at paradise.net.nz
Mon Jun 20 07:58:06 CEST 2005


Quoting Chuck Allison <chuck at freshsources.com>:

> Hello Tutors,
> 
> What would be the most Pythonic way of printing (or extracting) every 
> other element of a list? Thanks in advance.

This is probably it:

>>> arr = range(20)
>>> arr[::2]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> arr[1::2]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

-- 
John.


More information about the Tutor mailing list