Lambdas Python for loop

I wanted to use lambdas inside for loops to return if some elements are not numerical:

strs = ['1234', 'hello', '6787'] 

I can use a for loop and iterate over each element:

 for elem in strs: 

and check elem.islpha() .

However, is there a way to use lambdas in conjunction with a for loop to return the word "hello" ?

+7
source share
5 answers

Try this easily using list comprehension:

 lst = ['1234', 'hello', '6787'] [x for x in lst if x.isalpha()] => ['hello'] 

Or if you definitely want to use lambda:

 filter(lambda x: x.isalpha(), lst) => ['hello'] 

Note that you rarely use lambdas to filter inside a for loop, this is not idiomatic. The way to filter items from a list is to use lists, generator expressions, or (less recommended) the built-in filter() function.

+10
source

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'] 
+4
source

If you really need to use lambda , or you are learning functional programming, you can use filter :

 >>> filter(lambda x:x.isalpha(), ['123','hello','456']) ['hello'] 

Using lambda inside a loop does not make sense in your context, since you can directly call x.isalpha()

+1
source

This is probably what you are looking for. You cannot use a for loop explicitly, but you can use a generator expression:

 check_isalpha = lambda lst: (elem for elem in lst if elem.isalpha())[0] 
0
source

If you want to pull non-zero elements from the list, I don't see the need to use lambda.

 alphas = [item for item in strs if item.isalpha()] 
0
source

All Articles