Lambda Expressions & Anonymous Functions in Python

This is the article has some interest things, just think about that we are using a single action again and again it will take time and space complexity. So that we going to use functions, It provide better modularity for the application and a high degree of code reusing. We know that all that functions has one specific name for reusable. If u imagine without name we can code that function. Really it’s possible?

Sure, that’s the name of Anonymous functions, these functions are using lambda in-build python operator.

Let’s see some example:

This is what we are define the function. It’s works fine, let’s see how the Lambda has to the same,

It’s also like earlier function of x. Here lambda is not function name, It’s a python keyword that’s it.Just think, this function doesn’t has any name.

Just store that function result in one variable like this and return the result.

We can use multiple inputs in Lambda Expression

For example, there is a input for a login function to give first and last name.


> full_name = lambda fn,ln: fn.strip().title() + ” ” + + ln.strip().title()

>>full_name(” anand”, ” RAJ”)

‘Anand Raj’


Here we get two inputs are fn & ln, the strip() is used to remove the unwanted white space.

Using the title() to capitalize the first letter of the word.

Lambda Expressions

lambda : “What is lambda?”              # No input

lambda x: 3*x +1                                  # Single input

lambda x, y: (x*y)**0.5                      #Geometric mean

lambda x, y, z: 3/(1/x,1/y,1/z)            #Harmonic mean


lambda x1 ,x2 ,x3, ….., xn: <Expression>


We can’t use lambda expression in multi line functions.

For more example we can do some sort function using lambda,

I have a list of names with first name, last name and middle and sort it based on last name.

        >>> friends_name = [“Prakash A”, “Azharudeen M F”, “Praveen Kumar”,”Anand raj”,”Beer                         Mohamed”,”Arun kumar”,”Saravanan N G”]
        >>> help(friends_name.sort)
        Help on built-in function sort:

         sort(…) method of builtins.list instance
                  L.sort(key=None, reverse=False) -> None — stable sort *IN PLACE*

        >>> friends_name.sort(key=lambda name: name.split(” “)[-1].lower())   
        >>> friends_name
        [‘Prakash A’, ‘Azharudeen M F’, ‘Saravanan N G’, ‘Praveen Kumar’, ‘Arun kumar’, ‘Beer                        Mohamed’, ‘Anand raj’]

In this code, friend_name is a list have multiple names contains first, last and middle names.

friends_name.sort(key=lambda name: name.split(” “)[-1].lower())

here, key is the function argument for sorting to pass lambda expression using this key.

we split the name using split(” “) function, split the words using the white space.

Access the last name when using index in [-1]  and make all the words as lowercase because of avoiding case sensitive while sorting.

This is how we sorting a list using function but not using function name.


Praveen Kumar

I am a fullstack web developer with a passion for SEO, creating stunning websites, and organic marketing. I am a perfectionist.

Post navigation