Python Forum
lamda filter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: lamda filter (/thread-1869.html)



lamda filter - bluefrog - Jan-31-2017

Hi

I am attempting to understand lamdas.

number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)
output:
[-5, -4, -3, -2, -1]
Could somebody translate the above lamda to a old style for loop?


RE: lamda filter - wavic - Jan-31-2017

https://python-forum.io/Thread-Lambda-How-Why-and-Why-not


RE: lamda filter - Ofnuts - Jan-31-2017

lambda x: x < 0 is just a shorthand to create an anonymous function:

def negative(x):
   return x<0
filter(function,iterable) is a generator that applies function() to each element of iterable, and keeps those for which function returns true.


RE: lamda filter - micseydel - Jan-31-2017

(Jan-31-2017, 04:12 PM)bluefrog Wrote: Could somebody translate the above lamda to a old style for loop?
If you give it a try, we can correct any errors or misconceptions. If you're not sure how though, it's definitely worth making the attempt yourself.