Python Lambda – Anonymous Function Tutorial with Examples – POFTUT

Python Lambda – Anonymous Function Tutorial with Examples

Lambda functions are special types of functions that are not expressed as regular functions. Lambda functions do not have any function name to call and they are created for single line simple functions. The lambda function will eliminate the def keyword for a function definition.

Lambda functions are also called a small anonymous function where it does not have any function name. In the following part of the tutorial, we will see that lambda functions can take different types of arguments.

Lambda Function Use Characteristics and Use Cases

  • As a single line function definition it only contains expressions and does not contain any statements in its body.
  • It is written as a single line of execution
  • Lambda functions are easy to read because of its simplicity and atomicity.
  • Lambda functions do not support type annotations.
  • Lambda functions can be immediately called after definition by adding parameters in the same line.

Syntax of Lambda

The syntax of the Lambda function definition is very different than the other Python keywords and structures.

  • VAR is the variable name where the lambda function will be assigned.
  • lambda is the keyword used to define and create a lambda function
  • ARG1 ,ARG2 , … are arguments for the lambda function. We can use a single argument or multiple arguments. As they are optional we can also provide zero argument.
  • EXPRESSION ist the body of the lambda function which will be executed every time the lambda function is called

Lambda Example

We will start with a simple example where we will provide a single argument to the lambda function. We will provide the argument name as a and the lambda function will be assigned to the x.

LEARN MORE  How To Filter Python List, Dictionary, Array, String List, Object Tutorial with Examples?

Lambda Example

Lambda Example

  • In the first example, we will provide the “poftut.com” to the lambda function and the function will return the string “This is the poftut.com”.
  • In the second example, we provide 3 to the lambda function and it is multiplied 3 times with itself.
  • In the second example, we provide 4 to the lambda function and it is multiplied 3 times with itself.

Lambda without  Argument

We can use the lambda function without providing any argument. We will just put the expression part of the lambda function and use the newly created Lambda function. In this example, we will create the lambda function and assign it to the x where the function will print “This is the X”  to the screen.

Lambda without  Argument

Lambda without  Argument

Call Lambda Function Directly

Lambda function can be also called directly without assigning it to a variable. We will just provide the parameters after the lambda function definition. We will also surround the lambda function and provided arguments with brackets. In the following examples, we will do math and print “This site is poftut.com”.

Call Lambda Function Directly

Call Lambda Function Directly

Lambda with Multiple Arguments

Lambda functions can be used without argument or a single argument or multiple arguments. We can provide multiple arguments in a row which is also called positional arguments. In this example, we will provide arguments a as 1 , b as 2 and c as 5 like below.

LEARN MORE  Python reduce() Function Tutorial with Examples

Lambda with Multiple Arguments

Lambda with Multiple Arguments

Lambda with Named Arguments

We can use named arguments in lambda functions. We will set the c value as 5 by default. If the c is not provided to the lambda function call it will be assumed as 5. If provided the provided new value will be used.

Lambda with Named Arguments

Lambda with Named Arguments

Lambda with Variable List Of Arguments

If the provided arguments list and the count are variable we have to use args keyword which can map variable-length arguments. We will use *args as variable-length arguments and use inside the expression.

Lambda with Variable List Of Arguments

Lambda with Variable List Of Arguments

Lambda with Variable List Of Keyword Arguments

We can also use a variable list of keywords inside the Lambda functions. We will use kwargs keyword and select their values with the values() function. We will provide the arguments in different names.

Lambda with Variable List Of Keyword Arguments

Lambda with Variable List Of Keyword Arguments

Lambda with Python Interactive Shell

Lambda provides special usage with the Python interactive shell. We can create a lambda function without a variable assignment and call it later with the underscore _ operator. But we can only call the lambda function for one time after definition. If we try to call the lambda function the second time after the definition we will get an exception.

LEARN MORE  How To Filter Python List, Dictionary, Array, String List, Object Tutorial with Examples?

Lambda with Python Interactive Shell

Lambda with Python Interactive Shell

Lambda with filter() Function

filter() function is used to filter a sequence of variables according to the situation which is defined in a function. Lambda functions are very useful to create a situation for filtering. Using the lambda function will also make the filter more readable.

Lambda with filter() Function

Lambda with filter() Function

Lambda with map() Function

map() function is used to run a given function on the given list elements one by one. Lambda function another popular use case is using it with given list elements by using map() function. In this example, we will use lambda function in order to calculate square of the given list elements one by one.

Lambda with map() Function

Lambda with map() Function

Lambda with reduce() Function

reduce() is another interesting function wherefrom the given list multiple elements can be processed by the given function. As a function lambda can be used very efficiently for simple operations. In this example, we will sum for 2 neighbor elements in each step or reduce.

Lambda with reduce() Function

Lambda with reduce() Function

Leave a Reply

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

Enjoy this blog? Please spread the word :)