How to use Python Lambda – with example

  • Reading time:16 mins read

What is the Python Lambda function?

Lambda functions are essentially small and anonymous functions used in python code to perform specific tasks. Lambda functions only have one expression but can have any number of arguments. 

Compared to a standard user-defined function lambda functions have a few more constraints.

 

 

So to write a Python lambda expression or function, we would do the word lambda followed by x which is the parameter/argument, a semicolon and then an expression.

 

 

So to write this using the normal user-defined python function, we would do def, followed by the name of the function which in this case we will call twice. We would then pass x as a parameter and return twice its value.

 

 

In a Python lambda function, we would do exactly the same thing. So basically lambda functions can be of use in cases where we do not want to repeat over and over again and they fit in different parts and inside things like the map and the filter functions when working with lists. Using lambda we can write a function with the same output like the one shown above.

 

 

Python Lambda functions with lists

Now, generally speaking, you wouldn’t want to assign the name of the function every other time because that resembles writing a full function in python. Such kinds of lambda functions only fit into other functions such as the map() and the filter() functions in python or other places like that.

However, we can use these functions in more realistic applications such as when working with lists.

We are going to use a list of animals= [‘elephant’, ‘monkey’, ‘giraffe’, ‘tiger’, ‘shark’]. So what we could quite conceivably want to do is manipulate this list in a specific way and return out another list. For example, we can write a lambda function that turns all the lowercase words into uppercase words. 

 

Learn how to use Python Lambda online!

 

But since we just want to do this once and we don’t need to do it over and over again, we could use a lambda function for this. 

We need to create a new list calledanimalnew which we can then equate to the keyword list with a pair of parentheses, we’re also going to use the map keyword here. 

In Python the map function lets programmers apply a function to an iterable object.

Generally, the map function lets us apply our lambda function to the list and every item in our list. So within the parentheses of the map function, we will write map(lambda x: x.upper). And then finally the iterable list that we want to apply it to.

 This is going to create another list of the output. In this case, using the map function we are able to apply the lambda function which takes every member of the animals list and applies x.upper() to it thus capitalizing them.

 

 

Now, this is particularly useful when working with data frames using pandas because then we can apply the lambda function without having to write out full functions when you do not need them.

                                         Python Lambda is a useful tool in coding.                                                                                                              

 

Using the filter function

We can also use the filter function to filter words from a list that meet given criteria. In this example, we are going to return another list called animalsnew. However, in this case, we are not going to apply the x.upper(), instead, we will use ‘e’ in x.

This method allows us to take each word from that animals list that contains the letter ‘e’ into the animalsnew list leaving out the word shark that does not contain the letter ‘e’.

 

Summary

In conclusion, the Python lambda function makes your code much easier to read and are generally useful when used alongside other functions such as map and filter when iterating through simple objects. 

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.