First, get used to listing concepts:
>>> strs = ['1234', 'hello', '6787'] >>> [s for s in strs if s.isalpha()] ['hello']
They are the most adaptable and in most cases are the most readable ways. Then why use map ? map quick and easy search for simple cases:
>>> map(str.upper, strs) ['1234', 'HELLO', '6787']
But the instance you have to use lambda , the card becomes ugly and slow . This rule also applies to filter .
In [1]: strs = ['1234', 'hello', '6787'] In [2]: %timeit map(lambda s: s[0] if s.isalpha() else s, strs) 1000000 loops, best of 3: 1.48 us per loop In [3]: %timeit [s[0] if s.isalpha() else s for s in strs] 1000000 loops, best of 3: 799 ns per loop
Many people suggested
>>> filter(lambda s: s.isalpha(), strs) ['hello']
But remember that lambda s: s.isalpha() is just fuzz for str.isalpha , a method of the string class.
>>> filter(str.isalpha, strs) ['hello']
This makes it clean and quick to use filter here, but be aware of the consequences when you enable lambda s.
Python 2.x implementation details
Unfortunately, python 2.x has two different types of strings (in python 3 there is only str , which is unicode) str and unicode . Therefore, a clean solution using str.isalpha will not work. You can just change this to unicode.isalpha , but if you mix unicode and str , you need something cross compatible.
One option is to use methodcaller
>>> strs = [u'\n', u'test', u'\t'] >>> from operator import methodcaller >>> filter(methodcaller('isalpha'), strs) [u'test']
But this is not very simple, therefore it is useful to simply use lists to avoid this problem.
>>> [s for s in strs if s.isalpha()] [u'test']