A Guide to Understanding Python Lambda Functions

Nishant Singh
Data Analyst
Lambda functions (also known as anonymous functions) can feel a bit abstract at first, but once you understand it, they become one of the most powerful tools in your Python toolkit especially for data analysis related tasks.
I don't know if abyone is going to read this blog or not, but I am jusing eriting this so I can take a quick overview of the topic when needed. As lambda function is something that is not used very often so I always tend to forget the synatx. I know that syntax is something that can be googled within a second but I always prefer to have my own notes.
What is Lambda Function?
In Python, a lambda function is a small, restricted function that can be defined without a name. While a normal function is defined using the def keyword, a lambda function uses the lambda keyword. It can be seen as a way of looking cool but it's a practical way of writing functions specially if they need to be disposable or just one liner.
Syntax
lambda arguments: expression
lambda: The keyword that starts the function.arguments: The inputs (like ).expression: A single piece of code that is executed and returned.
Taking a simple addition as example
- User Defined Function
def add(x, y): return x+y print(add(10, 2)) #Output: 12 - Lambda Function
add_lambda = lambda x, y: x + y print(add_lambda(5, 3)) # Output: 8
Basic side-by-side comparison
| Feature | User-Defined Function (def) | Lambda Function (lambda) |
|---|---|---|
| Name | Has a formal name. | Anonymous (no name). |
| Keywords | Uses def and return. | Uses lambda; return is implicit. |
| Complexity | Can have multiple lines, loops, and complex logic. | Restricted to a single expression. |
| Reuse | Designed to be reused throughout a program. | Usually used once as an argument to another function. |
| Readability | Better for long, descriptive logic. | Better for "one-liners" that keep code concise. |
Why are Lambda Functions Important?
So as dicsussed lambda function is just a concise way of writing functions that are one liners right? Then why is there so much hype around it? Why can't we just write def and call it a day.
- Conciseness: They reduce "boilerplate" code. You don't have to name and define a function miles away from where you actually use it.
- Higher-Order Functions: Python has functions like
map(),filter(), andsorted()that require another function as an input. Lambdas are the perfect "plug-and-play" components for these.
For Example Let's assume we have to sort a list of tuples (Name, Age), and you want to sort them by age.
Using Lambda function we will just have to:
students = [("Alice", 25), ("Bob", 20), ("Charlie", 23)]
# We tell sorted() to look at index 1 (the age) for sorting
sorted_students = sorted(students, key=lambda x: x[1])
print(sorted_students)
# Output: [('Bob', 20), ('Charlie', 23), ('Alice', 25)]
But instead if we choose to use a UDF (User Defined Function) then:
students = [("Alice", 25), ("Bob", 20), ("Charlie", 23)]
# 1. Define the function
def get_age(student_tuple):
# This function takes one tuple and returns the element at index 1 (the age)
return student_tuple[1]
# 2. Pass the function name to the 'key' parameter
# Note: We don't use parentheses get_age() here; we just pass the function itself.
sorted_students = sorted(students, key=get_age)
print(sorted_students)
# Output: [('Bob', 20), ('Charlie', 23), ('Alice', 25)]
As you can we we had to make a separate get_age function as functions like sorted(), map() etc require a function as an input. So defining a separate function for them, which may not even be used further in the code doesnot make any sense.
As I am a Data Analyst a Pandas example is a must, so lambda function is never used by itself ever in DA but it is used in Pandas for cleaning and transforming the data.
Here we will take a simple sales dataset and will apply 10% taxto every row where price > 100.
import pandas as pd
# Sample Data
data = {
'Product': ['Laptop', 'Mouse', 'Monitor', 'Keyboard'],
'Price': [1200, 25, 300, 45]
}
df = pd.DataFrame(data)
# Using Lambda with .apply()
# Logic: If price > 100, add 10% tax, else keep original price
df['Taxed_Price'] = df['Price'].apply(lambda x: x * 1.10 if x > 100 else x)
print(df)
Yes it's that simple, now imagine writing a whole def calculate_tax(price): ... just for this small task. The lambda keeps the data transformation pipeline clean and easy to read.