Using wild character

Amit Khemka khemkaamit at
Thu Sep 6 07:40:57 CEST 2007

On 9/6/07, Sreeraj <sreeraj22283 at> wrote:
> hi,
>  I am a beginner in Python. I wish to know how can i filter a list of
> strings using wild
> 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
>>> al
['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 !


Amit Khemka

More information about the Python-list mailing list