Hackforge Academy

Category: python

Introduction to Lambda Functions

Published on 24 Jul 2026

Explanation

A lambda function is an anonymous (unnamed) function in Python that is defined using the lambda keyword. Lambda functions are

Code:

# Lambda function to add two numbers
add = lambda a, b: a + b
print(add(10, 20))

# Lambda function to find square
square = lambda x: x * x
print(square(5))

# Lambda function to find maximum
maximum = lambda a, b: a if a > b else b
print(maximum(25, 40))

Explanation

The map() function applies a given function to every element in an iterable such as a list or tuple and

Code:

numbers = [1, 2, 3, 4, 5]

# Square each number
squares = list(map(lambda x: x * x, numbers))
print(squares)

# Convert strings to integers
values = ['10', '20', '30']
integers = list(map(int, values))
print(integers)

# Convert names to uppercase
names = ['john', 'alice', 'david']
upper_names = list(map(str.upper, names))
print(upper_names)

Explanation

The filter() function is used to select elements from an iterable that satisfy a specified condition. It takes two arguments:

Code:

numbers = [10, 15, 20, 25, 30, 35, 40]

# Filter even numbers
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)

# Filter numbers greater than 20
greater_than_20 = list(filter(lambda x: x > 20, numbers))
print(greater_than_20)

names = ['John', '', 'Alice', '', 'David']
valid_names = list(filter(lambda name: name != '', names))
print(valid_names)

Explanation

The reduce() function repeatedly applies a function to the elements of an iterable until a single value is produced. It

Code:

from functools import reduce

numbers = [10, 20, 30, 40, 50]

# Sum of numbers
total = reduce(lambda a, b: a + b, numbers)
print('Total:', total)

# Product of numbers
product = reduce(lambda a, b: a * b, numbers)
print('Product:', product)

# Maximum number
maximum = reduce(lambda a, b: a if a > b else b, numbers)
print('Maximum:', maximum)

Explanation

Lambda functions, map(), filter(), and reduce() are often used together to process collections efficiently. A common workflow is to filter

Code:

from functools import reduce

prices = [1000, 2500, 5000, 7500, 900]

# Select products costing at least 2000
filtered_prices = list(filter(lambda price: price >= 2000, prices))
print('Filtered:', filtered_prices)

# Apply 10% discount
discounted_prices = list(map(lambda price: price * 0.9, filtered_prices))
print('Discounted:', discounted_prices)

# Calculate final bill
final_amount = reduce(lambda a, b: a + b, discounted_prices)
print('Final Amount:', final_amount)

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community