🤔 Lambda functions in Python

What are they? Where to use them? How to use them?

🧵Let's find out👇
1️⃣ Lambda function has its origin in A. Curch's idea of lambda calculus.

https://en.wikipedia.org/wiki/Lambda_calculus

In Python it consists of:
keyword - lambda
bound variable - x
body - x + 1 (always single expression)

It's also referred to as Anonymous function
2️⃣ You can use any variable defined inside the function's scope and bound variables inside lambda's body

It can be executed right away - you just need to wrap it with parenthesis and call it the same way as a regular function
3️⃣ It can be used with higher-order functions like map, reduce, filter.

Although they can be used, it's preferred to use list comprehension where you can
4️⃣ It can be used with key functions like sorted, min, and max.
You can follow @jangiacomelli.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.