Sitemap

Lambda Function in Python

4 min readAug 23, 2024

--

What is a Lambda Function?

A lambda function is a small, one-line function defined without a name. It’s called “lambda” because it’s defined using the lambda keyword. These functions can have any number of input parameters, but they have only one expression. The result of this expression is what gets returned.

Syntax of a Lambda Function

The basic syntax of a lambda function is:

Press enter or click to view image in full size

Here’s what each part means:

  • lambda: This keyword is used to create a lambda function.
  • arguments: These are the input parameters. A lambda function can take any number of arguments, separated by commas.
  • expression: This is the code that runs when the function is called. It's usually a simple operation that returns a value.
Press enter or click to view image in full size

Example of a Lambda Function

Let’s look at a simple example to make this clearer.

Press enter or click to view image in full size

In this example, lambda x, y: x + y creates a lambda function that takes two arguments, x and y, and returns their sum. We assign this function to a variable called add, and then we can use add just like a normal function.

Why Use Lambda Functions?

Lambda functions are useful in situations where you need a simple function for a short period of time, and you don’t want to write a separate named function for it. They are commonly used in functions like map()and filter(), where you need to provide a small function to be applied to a list or other iterable.

The map() Function in Python:

The map() function in Python is used to apply a specific function to all the items in an iterable (like a list or tuple) and return a new iterable with the results. This can be very helpful when you want to transform all elements in a list without using a loop.

Basic Syntax of map() :

Press enter or click to view image in full size
  • function: This is the function that you want to apply to each item in the iterable.
  • iterable: This is the list, tuple, or any other iterable that you want to process.

Simple Example with map()

Let’s say you have a list of numbers, and you want to square each number in the list. You can do this easily with map().

  1. Use map(): Next, use the map() function to apply the square() function to each element in the list.
Press enter or click to view image in full size

2. Convert to List: The map() function returns a map object, which is an iterator. To see the results, you need to convert it into a list.

Press enter or click to view image in full size

Using map() with a Lambda Function

You can also use a lambda function with map() for simpler operations. A lambda function is an anonymous function defined with the lambda keyword.

Get jegan A’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

Press enter or click to view image in full size

In this example, lambda x: x ** 2 is a lambda function that squares a number, and map() applies it to each number in the numbers list.

The filter( ) Function in Python :

The filter() function in Python is used to filter out elements from an iterable (like a list, tuple, etc.) based on a condition. It applies a function to each element of the iterable, and only the elements for which the function returns True are included in the result.

Basic Syntax of filter() :

Press enter or click to view image in full size
  • function: A function that returns either True or False for each element in the iterable.
  • iterable: The iterable (like a list or tuple) whose elements you want to filter.

Simple Example of filter()

Let’s say you have a list of numbers and you want to keep only the even numbers.

  1. Use filter(): Use the filter() function to apply the is_even() function to each element in the list.
Press enter or click to view image in full size

2. Convert to List: The filter() function returns a filter object, which is an iterator. To see the results, convert it into a list.

Press enter or click to view image in full size

Using filter() with a Lambda Function

You can also use a lambda function with filter() for simpler cases. A lambda function is an anonymous function defined using the lambda keyword.

Example:

Press enter or click to view image in full size

In this example, lambda x: x % 2 == 0 is a lambda function that returns True if x is even and False otherwise. filter() uses this lambda function to keep only the even numbers in the list.

--

--