Lambda Function in Python

Sachin Chaudhary
Geek Culture
Published in
5 min readNov 26, 2021

Python lambdas are small, anonymous, sub-syntax works that are more limited but shorter than normal Python functions.

In this article we will see everything about lambda. We will discuss and try to answer the following question about lambda:

  • Where lambda came
  • What is lambda function
  • How to use lambda function
  • When to avoid lambda function
  • Lambda alternatives

Lambda is not a concept in python only, it is used in other programming languages such as Java, C#,C++ , etc. The concept of lambda in programming languages came from calculus mathematics.

Where lambda came

History of lambda starts back in 1930’s. Alonzo Church, best known for lambda calculus, Church–Turing thesis, was an American mathematician and logician formalised lambda calculus. Functional languages ​​derive their roots from mathematical logic and lambda calculus, while key programming languages ​​adopt a state-based calculation model developed by Alan Turing. Two calculation models, lambda calculus and Turing machines, can be translated into one. This similarity is known as the Church-Turing hypothesis.

Functional languages like Haskell and Lisp, inherit the lambda calculus but languages like Python or C inherit imperative programming.

Initially, Python was not a functional language but in later stages it adopts few functional concepts. Lambda function is one of the adoption including some other function like reduce( ), map( ) and filter( ).

I can write more about history of lambda but that is not our main topic of discussion. So moving further on next topic.

What is lambda function

Lambda Functions are anonymous function in Python. Lambda functions are similar to regular functions. The difference between regular function and lambda function is that they can be defined without a name but the normal functions are defined with def keyword.

lambda keyword is used to define an anonymous or lambda function.

If we compare lambda function and normal function then,

  1. A lambda function can take any number of arguments, but can only have one expression while regular function have exact number of arguments that we declare at the time of defining.
  2. Lambda functions are one line functions. its body contain expression in the same line in which it is defined. In normal functions, functions contains body blocks in which some statements are defined to execute.
  3. As lambda is a single line function so it can be invoke instantly but normal function needs to call itself and take time to call.

The above code shows the lambda function which gives product of two variable x and y. In the above example x is variable which will store the return value of lambda expression.

Parts of Lambda expression.

Keyword - lambda.

arguments - x and y.

expression - x*y

We can pass the arguments to function by surrounding the function and its argument with parentheses:

Below is the example of passing arguments within same single line.

You can write lambda without variable name as well. So here we understood that lambda function can be write without name and variable.

How to use lambda function

Till now I gave all the theory of lambda so we will understand that how to use lambda with the help of examples.

Example.1 Write a program to calculate exponent using lambda function.

calculate exponent

To solve the above example I created a very beautiful visualisation to understand the process. You can think in this manner like ..

  • First I am defining the lambda expression to calculate exponent.
  • Pass that expression to lambda function represented by a square box.
  • Lambda function is represented by a box. It will give you the output after doing calculation.
  • The code is below.

I took different values but that is not a problem.

Now I want to discuss one more thing here which is very important. Only few coders discuss this thing. To do that I am taking another example.

First we understand the code.

  • I just defined a string and store in variable ‘name’ that passed to the lambda as a parameter.
  • After that declare a lambda that prints the result.
  • But I did not get result.

This is because the lambda itself returns a function object. In the above example, the lambda is simply returning the function object and the memory location where it is stored. And the output we get at the console is that location.

Now I will do some changes to get desired output.

Explanation of code:

Now I got what I want from lambda but there is one more output that is ‘None’.

Now when I run the code, my outer print function is trying to call lambda function and inner print function is calling name variable. So inner print is giving me the string and outer print returns the object from lambda function.

Now It gives me correct output which I want in both ways.

Look how hectic this task was to print a statement using lambda.

Here you need to understand this thing When to use lambda and when to avoid.

Now I will write the same code with regular function.

How easy it is ?

When to avoid lambda function

So the thing is we need to avoid lambda for simple tasks and understand the usage of lambda.

If I compare the differences here.

When we were using lambda function, we did not use any function name, and there was no need to call lambda function explicitly, that is the reason lambda is called anonymous function.

For regular function we define a function name print_line and there is body as well but in lambda function case there was no body. That was single lined.

These are some points against the lambda function use in Python are:

  • Code readability
  • force to think in functional way
  • Weird syntax
  • Cryptic style

Lambda Alternatives

  • List comprehension.
  • Generators.

List comprehension is the best alternate to lambda. There are some cases when you use lambda with map( ), reduce( )and filter( ) functions but the same thing could be completed with list comprehension along with for loop.

Now you can think on your own how and when to use lambda functions.

I am hoping that this article will help beginner to understand lambda introduction.

--

--

Sachin Chaudhary
Geek Culture

Computer Science Student| Junior Data Scientist| Learner