Extract all words that begin with x

Xavier Ho contact at xavierho.com
Mon May 10 04:50:56 EDT 2010


Have I missed something, or wouldn't this work just as well:

>>> list_of_strings = ['2', 'awes', '3465sdg', 'dbsdf', 'asdgas']
>>> [word for word in list_of_strings if word[0] == 'a']
['awes', 'asdgas']

Cheers,
Xav


On Mon, May 10, 2010 at 6:40 PM, Jimbo <nilly16 at yahoo.com> wrote:

> Hello
>
> I am trying to find if there is a string OR list function that will
> search a list of strings for all the strings that start with 'a' &
> return a new list containing all the strings that started with 'a'.
>
> I have had a search of Python site & I could not find what I am
> looking for, does a function like this exist?
>
> The only one that I think could work is, use the string
> function .count()
>
> algorithm: to exract all the words inside a list that start with 'a'
>  - make sure the list is arranged in alphabetical order
>  - convert the list to a big string
>  - use list_string.count(',a') to obtain the index where the last 'a'
> word occurs
>  - convert back into string (yes this is a REAL hack :P)
>  - and then create a new list, ie, new_list = list[0:32]
>  - & return new_list
>
> Ok that algorithm is terrible, I know, & I just realise that it wont
> work for letters after 'a'. So if anyone could suggest a function or
> algorithm it would be extremely helpful
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100510/eff35795/attachment.html>


More information about the Python-list mailing list