LISTS: Extract every other element

Magnus L. Hetland mlh at vier.idi.ntnu.no
Sat Dec 18 12:44:06 EST 1999


Charles G Waldman <cgw at fnal.gov> writes:

> Gerrit Holl writes:
>  > Randall Hopper wrote:
>  > > I want to take a large list:
>  > > 
>  > >   [ 1,2,3,4,5,6,7,... ]
>  > > 
>  > > and build a list with every other element:
>  > > 
>  > >   [ 1,3,5,7,... ]

How about:

def odd(pair): return pair[0] % 2 == 1
def second(pair): return pair[1]

lst = range(10)

result = map(second,filter(odd,map(None,range(len(lst)),lst)))


Quite intuitive ;)

--

  Magnus          Echelon jamming noise:
  Lie             FBI CIA NSA Handgun Assault Bomb Drug Terrorism
  Hetland         Special Forces Delta Force AK47 Hillary Clinton 



More information about the Python-list mailing list