Using wild character

TheFlyingDutchman zzbbaadd at aol.com
Thu Sep 6 01:54:55 EDT 2007


On Sep 5, 10:00 pm, Sreeraj <sreeraj22... at gmail.com> wrote:
> hi,
>
>  I am a beginner in Python. I wish to know how can i filter a list of
> strings using wild characters.ie
> Lets say i have list countries =
> ["india","africa","atlanta","artica","nigeria"]. I need only the list
> of string starting with 'a'.
>
> thank you
>
> Sreeraj

The most thorough answer would no doubt involve regular expressions,
but they can be unpleasant.

To do a "string*" type wildcard filter as in your request:

myList =  ["india","africa","atlanta","artica","nigeria"]

newList = [ item for item in myList if item.startswith("a") ]


To do a "*string" wildcard filter use the endswith() function instead
of startswith() and to do a *string* type wildcard filter use
the find() function > -1.




More information about the Python-list mailing list