TGViewer
Learn Python Coding Learn Python Coding @pythonre · 40.7K subscribers
Post #9462 2.12K
What is a Lambda Function?

A lambda function is a small anonymous function defined using the lambda keyword. It's often used for short, throwaway functions that are only needed temporarily.

Basic Syntax- The syntax of a lambda function is:
"lambda arguments: expression"

-arguments: A comma-separated list of parameters.
-expression: An expression that is evaluated and returned.


Examples
1️⃣ Basic Lambda Function:
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5

Here, lambda x, y: x + y is a lambda function that adds two numbers.

2️⃣ Lambda with map():
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(squared) # Output: [1, 4, 9, 16, 25]

map() applies the lambda function to each item in the numbers list.

3️⃣ Lambda with filter():
numbers = [1, 2, 3, 4, 5]
even = list(filter(lambda x: x % 2 == 0, numbers))
print(even) # Output: [2, 4]

filter() uses the lambda function to filter out only the even numbers.

4️⃣ Lambda with reduce():
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 120

reduce() applies the lambda function cumulatively to the items in the list.

Pros and Cons-

Pros:
-> Concise and readable.
-> Useful for small, simple functions.
-> Handy for functional programming (e.g., map, filter, reduce).

Cons:
-> Limited to single expressions.
-> Can be less readable if overused.
-> Lack of function name can make debugging harder.

Lambda functions are an excellent tool for any Python developer to have in their toolkit. They can help streamline your code and make your functions more elegant and efficient.
  • ❤ 2
More from @pythonre
  1. Oct 9, 20267 Days Certified AI Agents with Python: Autonomous Apps Build autonomous AI agents with Py…
  2. Oct 7, 2026A distinctive bot for subscribing to courses on the most popular educational platforms acr…
  3. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  4. Oct 7, 2026📱 270 tools for Python developers The repository contains: 🫡 Linters and formatters; 🫡…
  5. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
  6. Oct 6, 2026Post #9851
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →