Lambda in Python

Python Lambda Desmistification — #PurePythonSeries — Episode #00

J3
Jungletronics
3 min readAug 29, 2021

--

Fig 1. Lambda Expression: lambda function:iterable

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:

The times2 function can be written in one line:

Rewriting it in one line:

Now, get rid of the def keyword:

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);

And get rid of the parentheses adding space in front (cleaning the code:);

And finally, get rid of return word (its function is assumed by the colon ), like this :)

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):

Now use Map plus Lambda together:

# Using Map function plus Lambda function (what it is build for!):

# Casting Map to a List:

Complete code:)

👉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:)

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️