Introduction

Lambda functions in Python, also known as anonymous functions, are small, unnamed functions defined with the lambda keyword. They are useful for short, quick functions that you may need temporarily and typically involve simple operations.

This guide shows you how to use lambda functions in Python.

Prerequisites

Before you begin:

  • Deploy a VPS server. For instance, Ubuntu 24.04.
  • Create a non-root sudo user.
  • Install Python.

Define a Lambda Function

To define a lambda function, use the lambda keyword followed by the parameters, a colon, and the expression to be evaluated.

Here’s the basic syntax:

Python
lambda parameters: expression

Example:

Python
square = lambda x: x ** 2
print(square(5))  # Output: 25

In this example, the lambda function takes one parameter x and returns its square.

Use Higher-Order Functions with Lambda

Lambda functions are often used with higher-order functions, such as map(), filter(), and reduce().

Apply map()

The map() function applies a given function to all items in an iterable, such as a list.

Example:

Python
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
print(squared_numbers)  # Output: [1, 4, 9, 16, 25]

In this example, the lambda function squares each number in the list using map().

Filter Items with filter()

The filter() function filters items in an iterable based on a given function that returns True or False.

Example:

Python
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  # Output: [2, 4]

In this example, the lambda function filters out even numbers from the list using filter().

Reduce Values with reduce()

The reduce() function, available in the functools module, applies a given function cumulatively to the items in an iterable, reducing them to a single value.

Example:

Python
from functools import reduce

numbers = [1, 2, 3, 4, 5]
sum_of_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_of_numbers)  # Output: 15

In this example, the lambda function sums up all the numbers in the list using reduce().

Use Default Parameters in Lambda Functions

Like regular functions, lambda functions can have default parameters.

Example:

Python
add = lambda x, y=10: x + y
print(add(5))  # Output: 15
print(add(5, 3))  # Output: 8

In this example, the lambda function add has a default parameter y with a value of 10.

Apply Lambda Functions in Practical Use Cases

You'll commonly use Lambda functions in simple operations that require to pass arguments to higher-order functions or when sorting.

Example for sorting a list of tuples:

Python
students = [("Alice", 25), ("Bob", 22), ("Charlie", 23)]
sorted_students = sorted(students, key=lambda student: student[1])
print(sorted_students)  # Output: [('Bob', 22), ('Charlie', 23), ('Alice', 25)]

In this example, the lambda function sorts the list of tuples based on the second element (age) of each tuple.

Conclusion

This guide explains how to use lambda functions in Python, including defining lambda functions, using them with higher-order functions like map(), filter(), and reduce(), handling default parameters, and practical use cases. Lambda functions are a powerful and concise way to perform small operations without the need for fully defined functions.