Lambda Function in Python (With Exmaples)

lambda-function-in-python

In Python, a lambda function is a small, anonymous function that can be defined on a single line.

It is also known as a “lambda expression” or “anonymous function”.

Lambda functions are typically used when you need a small, one-time-use function that you don’t want to define with a separate def statement.

Lambda functions are defined using the lambda keyword, followed by the function’s input arguments and a colon, and then the expression that the function should evaluate and return.

Here is the basic syntax of a lambda function in Python:

lambda arguments: expression

For example, let’s say you want to define a function that squares a number. You could do this with a regular def statement:

def square(x):
    return x ** 2

Or, you could use a lambda function:

square = lambda x: x ** 2

The lambda function takes a single argument (x) and returns the square of that argument.

Note that we assigned the lambda function to a variable (square) so that we can use it later.

Lambda functions can take any number of arguments, and the arguments can be of any data type.

Here is an example of a lambda function that takes two arguments and returns their sum:

sum = lambda a, b: a + b

You can use lambda functions anywhere that you would use a regular function, such as as an argument to another function or as a value in a dictionary.

For example, you could use a lambda function as the key function when sorting a list:

numbers = [4, 2, 1, 3, 5]
sorted_numbers = sorted(numbers, key=lambda x: -x)

In this example, the sorted() function is called with a key argument that is a lambda function.

The lambda function takes a single argument (x) and returns its negation.

This causes the numbers in the list to be sorted in reverse order.

What is the lambda function in Python?

A lambda function is a small, anonymous function that can be defined on a single line. It is also known as a “lambda expression” or “anonymous function”.

Lambda functions are used when you need a small, one-time-use function that you don’t want to define with a separate def statement.

Why do we use the lambda function?

Lambda functions provide a simple and convenient way to define small, anonymous functions in Python.

They can be used to make your code more concise and readable.

They are particularly useful for functional programming and for situations where you need to define a function for a single use.

What is the benefit of the lambda function in Python?

Some other benefits include:

Concise code

Lambda functions are designed to be used for short, simple operations that can be expressed in a single line of code.

For example, the cube of any number can be found using this lambda function.

cube = lambda x: x ** 3

They can make your code more concise and readable by eliminating the need for a separate def statement.

Functional Programming

Lambda functions are commonly used in functional programming, which is a programming paradigm that emphasizes the use of functions as the primary building blocks of a program.

Lambda functions allow you to pass functions as arguments to other functions and return functions as values.

Anonymous Functions

Lambda functions are anonymous, meaning they don’t have a name.

lambda argument: expression
#if you pass x as an argument and want to determine if square then lambda function will be
lambda x: x**2 

This makes them ideal for situations where you must define a function for a single use, such as when sorting a list or filtering data.

Speed

Sometimes, a lambda function can be faster than a named function because it eliminates the overhead of defining and looking up a function by name.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x