Python Forum

Full Version: lamda filter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.
(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.