When and why should I use lambda functions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

When and why should I use lambda functions?

I'm trying to figure out how and when Python lambdas should be used, and why should I use them instead of regular functions.

22nd Apr 2017, 10:38 AM
Dawid Zietal
Dawid Zietal - avatar
6 Answers
+ 11
when you are in hurry or near deadline.
22nd Apr 2017, 10:58 AM
Agus Mei
Agus Mei - avatar
+ 10
When there is no point in defining a regular, named function, i.e. if you only use it once - as a key in sorted() or in map(), filter(), reduce() situations.
22nd Apr 2017, 11:11 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
22nd Apr 2017, 10:45 AM
Ohjel
Ohjel - avatar
+ 2
# I find they are handy to # keep simple operations close # to associated code sometimes import tkinter as tk root = tk.Tk() tk.Button(text='Double click Me').grid() b=root.winfo_children()[0] b.bind("<Double-1>",lambda event:b.configure(text='clicked twice')) tk.Button(text='text here', command=lambda:anon_click_command('top')).grid() tk.Button(text='text here', command=lambda:anon_click_command('bottom')).grid() def anon_click_command(button_id): c= root.winfo_children()[1:] for but in c: if but['text']!='text here': but['text']='text here' if button_id =='top': c[0].configure(text='changed top') elif button_id =='bottom': c[1].configure(text='changed bottom') root.mainloop()
22nd Apr 2017, 11:41 AM
richard
+ 1
@Agus damn true 😂😂😂
23rd Apr 2017, 3:12 AM
Tamoghna Saha
0
List Comprehensions 5.1.3 says that Lambda functions can be used to calculate List Comprehensions without any "side effects". (https://docs.python.org/3/tutorial/datastructures.html)
3rd Jul 2017, 1:50 AM
Fattig Edwin
Fattig Edwin - avatar