Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lamda filter
#1
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?
Reply
#2
https://python-forum.io/Thread-Lambda-Ho...nd-Why-not
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,538 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  Lamda function explanation mrcool4 4 3,492 Jul-04-2018, 10:44 AM
Last Post: mrcool4

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020