Python – Lambda Functions

By | 28/05/2025

In this post, we will see what the Lambda Functions are and how we can use them in our projects.
But first of all: what are “Lambda functions”?
“Lambda Functions are small, anonymous functions defined with the “lambda” keyword.
Unlike regular functions defined with “def”, a Lambda Function is typically one line of code, used to perform simple operations without the overhead of defining a standard function with a name. While they are often used as inline functions passed as arguments to higher-order functions like “map()”, “filter()” or “sorted()”, they can be a handy shortcut for quick, throwaway functions in many parts of our code.”


Lambda Functions come with a few characteristics and restrictions:

  • They can only contain a single expression (though that expression can be complex)
  • They return the value of that expression implicitly; no return statement is needed
  • They’re often used in places where a small function is required only once or where defining a full named function would feel unnecessarily verbose.

Now, let’s see some practical examples to illustrate the power of lambda functions:

Basic Mathematical Operation
This example demonstrates a basic lambda function that adds two numbers.

# Define a lambda function that adds two numbers
sum = lambda x,y: x + y

# Use the lambda function
result = sum(5,2)

print(f"The sum of 5 + 2 is:{result}")


Lambda Function with Map
The “map()” function applies a given function to all items in an iterable (like a list).

# Create a list of numbers
lst_numbers = [1,2,3,4,5]

# Using map to square each number in the list
lst_squared_numbers = list(map(lambda x: x*x, lst_numbers))

print(f"List: {lst_numbers}")
print(f"The squared List is: {lst_squared_numbers}")

# Create a list of lowercase strings
lst_numbers_str = ['one', 'two', 'three', 'four', 'five']

# Using map with a lambda to transform each word to uppercase
lst_numbers_str_upper = list(map(lambda item: item.upper(), lst_numbers_str))

print(f"List: {lst_numbers_str}")
print(f"The upper List is: {lst_numbers_str_upper}")


Lambda Function with Filter
The “filter()” function filters out numbers based on a condition.
In this case, the lambda checks if each number is even and, only even numbers pass the filter condition.

# Create a list of lowercase strings
lst_numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

# Using filter to get even numbers from the list
lst_numbers_even = list(filter(lambda item: item % 2 == 0, lst_numbers))

print(f"List: {lst_numbers}")
print(f"The List of even numbers is: {lst_numbers_even}")


Lambda Function for Sorting
Lambdas are useful as “” key functions” for sorting complex data structures.

# A list of (product_name, price) tuples
lst_products = [("Laptop", 1200),("Smartphone", 600),
    ("Tablet", 300),("Headphones", 100)]

# Sorting products by price using a lambda as the key
lst_products_sorted_by_price = sorted(lst_products, key=lambda item: item[1], reverse=True)

print(f"The list sorted by price is:{lst_products_sorted_by_price}")

# List of dictionaries {name: age}
lst_students = [ {'Damiano': 25}, {'Dorian': 22}, {'Rosalia': 23} ]

# Sorting the list of dictionaries by the 'age' key
lst_students_order_by_age = sorted(lst_students, key=lambda item: list(item.values())[0], reverse=True)

print(f"The list of students ordered by age is:{lst_students_order_by_age}")


Lambda functions are a valuable tool in Python for writing concise and expressive code, and they are especially useful in situations where we need a small function.



Leave a Reply

Your email address will not be published. Required fields are marked *