[Tutor] extracting elements in a sequence

Gonçalo Rodrigues op73418@mail.telepac.pt
Mon Feb 10 07:59:07 2003


----- Original Message ----- 
From: <alan.gauld@bt.com>
To: <fredm@smartypantsco.com>; <tim@johnsons-web.com>; <tutor@python.org>
Sent: Monday, February 10, 2003 10:45 AM
Subject: RE: [Tutor] extracting elements in a sequence 


> > As far as I know there is no built-in way to extract the nth 
> > element out of a list, but it is very simple to write a function 
> > to do this for you:
> > 
> def extract (oldlist, spacing):
>      newlist = []
>      for i in range(0,len(oldlist),spacing):
>         newlist.append(oldlist[i])
>      return newlist
> 

In the coming 2.3 (still in alpha) you may use extended slices, e.g.

newlist[::spacing]

> Should do it?
> 
> Alan g.

All the best,
G. Rodrigues