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 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 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 as 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 kind 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.
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 called “animalnew” 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 dataframes using pandas because then we can apply the lambda function without having to write out full functions when you do not need them.
Using the filter function
We can also use the filter function to filter words from a list that meet a 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’.
In conclusion, the 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 Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Best way of using Python Split
- Best way of using Python Try-Except
- Best way of using Python Tuple
- Best way of using Python Arrays
- Best way of using Python Sort
- Best way of using Python DateTime
- How to download Python?
- Best way of using Python FileWrite
- Best way of using Python Lambda
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.