Using wild character
khemkaamit at gmail.com
Thu Sep 6 07:40:57 CEST 2007
On 9/6/07, Sreeraj <sreeraj22283 at gmail.com> wrote:
> 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'.
There are a few ways of doing so. For some simple operations there are
functions on the strings, If you want some more complex "filtering"
then have a look at Python's regular expression module "re".
>>> l = ["india","africa","atlanta","artica","nigeria"]
>>> al = [c for c in l if c.startswith('a')] # this is a list comprehension
['africa', 'atlanta', 'artica']
To know more about list comprehensions, have a look at:
Methods on strings:
Btw, not all of names in your list are countries !
More information about the Python-list