[Tutor] Hmmm...

Britt Green britt_green@hotmail.com
Fri, 09 Mar 2001 00:01:54 -0800


>On Thu, Mar 08, 2001 at 07:03:09PM -0800, Britt Green wrote:
> > If I have a list, is there a way to extract from it the element that
> > contains a string?
> >
> > For example, if I have:
> >
> > someVeggies = ['carrot', 'lettuce', 'rhubarb', 'peas']
> >
> > how would i extract the element that matches the string 'carrot' or 
>'peas'
> > and put that into another string?
> >
>
>if 'carrot' in someVeggies:
>    print 'Veggies has a carrot'
>
>is that useful?

Actually, I figured out what I needed to do on my own!

import string

theFruits = ['apple', 'banana', 'pineapple', 'pear', 'orange']

chosen = []

for stuff in theFruits:
    if stuff.endswith('e'):
        chosen.append(stuff)

    print chosen


When run, only the elements of theFruits that end with an 'e' will get added 
to the list of chosen. Is there a better way to do this?

Britt



--
It is pitch black. You are likely to be eaten by a grue.

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com