Blog Post #106: One-Liners for Simple Tasks: An Introduction to lambda Functions

So far, we have been defining our functions using the def keyword. This is the standard way to create named, multi-line functions, and it’s perfect for most tasks. However, sometimes you need a very simple function that performs just one small operation, and you may only need to use it in one specific place.

For these situations, Python provides a way to create small, “anonymous” functions on a single line. These are called lambda functions.

In this post, we’ll learn the syntax for lambda functions and understand their role in writing concise Python code.

What is a Lambda Function?

A lambda function is a small, anonymous function defined with the lambda keyword. It’s characterized by a few key properties:

  • Anonymous: It doesn’t have a name in the same way a function defined with def does. It’s a “throwaway” function.
  • Single Expression: Its body can only contain a single expression, not a block of statements (so you can’t have multiple lines of logic or use statements like print inside it).
  • Implicit Return: The result of this single expression is automatically returned. You do not use the return keyword.
  • One-Liner: It’s written entirely on one line.

The lambda Syntax

The syntax for a lambda function is more compact than a def statement. It looks like this:

lambda arguments: expression

Let’s break that down:

  • lambda: The keyword that signifies you are creating an anonymous function.
  • arguments: A comma-separated list of parameters the function will accept (just like the parameters inside the parentheses of a def statement).
  • :: A colon that separates the arguments from the expression.
  • expression: A single expression that is evaluated and returned when the function is called.

lambda vs. def: A Direct Comparison

The best way to understand a lambda is to see it next to its def equivalent. Let’s create a simple function that adds two numbers.

The def version:

def add_numbers(x, y):
    return x + y

The lambda version:

lambda x, y: x + y

Both of these create a function that takes two arguments, x and y, and returns their sum. The lambda version is just much more compact.

While you can assign a lambda to a variable to give it a name and call it, this is generally discouraged by Python style guides. If a function is important enough to have a name, you should just use def.

# This works, but using 'def' is preferred for named functions.
add_lambda = lambda x, y: x + y
result = add_lambda(10, 5)
print(result) # Output: 15

The True Purpose of Lambda Functions

If assigning lambdas to variables is discouraged, what are they for?

Their primary purpose is to be used as small, disposable functions that are passed as arguments to higher-order functions (which we introduced in Post #104).

Functions like sorted(), and others like map() and filter(), can take another function as an argument to customize their behavior. A lambda is perfect for providing this custom behavior on the fly, without the need to define a full, separate def function that you’ll only use that one time.

What’s Next?

Lambda functions provide a concise syntax for creating small, single-expression anonymous functions. While they have their limitations (they can’t be complex), they are a powerful tool for writing clean and functional-style code when used appropriately.

We’ve now seen the theory behind lambda functions. To truly understand their power, we need to see them in action with the higher-order functions they were designed for. In Post #107, we will explore practical examples of using lambda functions with sorted(), map(), and filter().

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment