Lambda in Python
Python Lambda Desmistification — #PurePythonSeries — Episode #00
This post attempts to demystify Lambda in Python.
Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression) — From TowardsDatascience.com.
LAMBDA
How To Get A Lambda Expression just from times2 function?
Here is the original function:
def times2(var):
return var*2
The times2 function can be written in one line:
Rewriting it in one line:
def times2(var):return var*2
Now, get rid of the def keyword:
times2(var):return var*2
Now, to get a lambda expression just get rid of the name of the function (typing lambda in its place — because lambda is an anonymous function);
lambda(var):return var*2
And get rid of the parentheses adding space in front (cleaning the code:);
lambda var:return var*2
And finally, get rid of return word (its function is assumed by the colon ), like this :)
lambda var:var*2
It Reads: lambda takes var and returns var*2.
Just in one line. Simple Like That! Awesome!
This is not usual for lambda, but it is a feasible use (saving lambda times2 to var t2):
t2 = lambda var:var*2t2(5)10
Now use Map plus Lambda together:
# Using Map function plus Lambda function (what it is build for!):
seq = [1,2,3,4,5]
map(lambda var:var*2, seq)<map at 0x7f0640b37b38>
# Casting Map to a List:
list(map(lambda var:var*2, seq))[2, 4, 6, 8, 10]
Complete code:)
seq = [1,2,3,4,5]
print(list(map(lambda var:var*2, seq)))[2, 4, 6, 8, 10]
👉Jupiter notebook link :)
👉git
Credits & References
Lambda Functions with Practical Examples in Python — How, when to use, and when not to use Lambda functions by towardsdatascience.com
Related Posts
00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification (this one)
01#Episode#PurePythonSeries — Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python
02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook — How To Create An Email Trigger System in Python
03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!
04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review
05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month
06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension
07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python
08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function
10#Episode#PurePythonSeries — CS50 — A Taste of Python — Harvard Mario’s Challenge Solver \o/
11#Episode#PurePythonSeries — Python — Send Email Using SMTP — Send Mail To Any Internet Machine (SMTP or ESMTP)
12#Episode#PurePythonSeries — Advanced Python Technologies — qrcode, Speech Recognition in Python, Google Speech Recognition
13#Episode#PurePythonSeries — Advanced Python Technologies II — qFace Recognition w/ Jupyter Notebook & Ubuntu
14#Episode#PurePythonSeries — Advanced Python Technologies III — Face Recognition w/ Colab
15#Episode#PurePythonSeries — ISS Tracking Project — Get an Email alert when International Space Station (ISS) is above of us in the sky, at night
16#Episode#PurePythonSeries — Using Gemini Chat on Collab — Random Number Generation, List Manipulation & Rock-Paper-Scissors Game Implementations
17#Episode#PurePythonSeries — Python — Basics — Functions, OOP, file handling, calculator, loops
18#Episode#PurePythonSeries — Python — Efficient File Handling in Python — Best Practices and Common Methods
Edited at Dec_2021 — completing the code to run in pycharm:)