[Tutor] extracting elements in a sequence

Tim Johnson tim@johnsons-web.com
Sun Feb 9 17:17:02 2003


* Alfred Milgrom <fredm@smartypantsco.com> [030209 13:16]:
> Hi Tim:
 
 Hello Alfred:

> As far as I know there is no built-in way to extract the nth element out of 

  Aha, was just checking...

> a list, but it is very simple to write a function to do this for you:

> def extract (oldlist, spacing):
>     count = 0
>     newlist = []
>     for item in oldlist:
>         if count % spacing == 0:
>             newlist.append(item)
>         count = count+1
>     return newlist
 
  Yes, I just wrote something similar, but I like
  yours better. :-)
  thank you Alfred.
  regards
  -tim-

> oldlist = [1,'one',2,'two',3,'three']
> newlist = extract(oldlist, 2)
> print newlist
> newlist = extract(oldlist, 3)
> print newlist
> 
> (The % operator is the modulo function)
> 
> Hope this helps you,
> Alfred Milgrom

-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com
      http://www.johnsons-web.com