Extract all words that begin with x
Aahz
aahz at pythoncraft.com
Mon May 10 12:48:21 EDT 2010
In article <mailman.2840.1273484131.23598.python-list at python.org>,
James Mills <prologic at shortcircuit.net.au> wrote:
>On Mon, May 10, 2010 at 6:50 PM, Xavier Ho <contact at xavierho.com> wrote:
>> 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']
>
>I would do this for completeness (just in case):
>
>>>>> [word for word in list_of_strings if word and word[0] == 'a']
>
>Just guards against empty strings which may or may not be in the list.
No need to do that with startswith():
>>> ''.startswith('x')
False
You would only need to use your code if you suspected that some elements
might be None.
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
f u cn rd ths, u cn gt a gd jb n nx prgrmmng.
More information about the Python-list
mailing list