Lambda Functions in Python Explained
Connor Van Etten
Posted on April 22, 2023
Another fancy trick within Python, lambda functions add another way to execute a block of code in one line. So, what are lambda functions and how do we use them?
What are Lambdas?
Lambda functions are small anonymous functions that can take multiple arguments and perform a singular expression on them. The format for a lambda function involves the keyword "lambda", the argument names, and the expression itself after a colon (:).
Lambda Formatting
lambda argument(s): expression
While traditional functions generally have a return statement, a lambda will merely return the value of its expression after running. Let's take a look at an example of using a lambda.
(lambda a: a * 3)(4)
This example above will return the value 12 (4*3) when called. Note that since this is an anonymous function, we cannot call it by itself, and we have to encase the function in parentheses, and then the number in the parentheses to the right will be input. Now, this seems kind of pointless as the value given is not utilized. Now let's take a look at a way we can make this function more usable through your program.
lamb = lambda a: a * 3
print(lamb(4))
In this case, we add a variable to hold this lambda function, and therefore we are able to use it elsewhere. Using the same lambda function as our first example, we give that add a variable to hold the function. Then we can use the variable name to run our lambda function from it. Again, this idea will not often be used as it may be more readable to write a full function and just call that for this use case, but it is important to understand you can do this. Now let's look at two of the more useful and common ways to use a lambda.
Lambda Functions with Iterables
example_list = [1, 2, 3, 4, 5, 6, 7, 8]
example_list = list(map(lambda x: x**2, example_list))
print(example_list)
output => [1, 4, 9, 16, 25, 36, 49, 64]
The biggest and most helpful use case of a lambda function would be with Iterables. Above we see an example of wanting to work our way through a list and replace the value of each element with its squared value. If you are unfamiliar with the map function, its purpose is to take in a function as its first argument and performs it on all the elements in the second argument and returns an object that can be set to a list or tuple. In the example above, we could define a separate function or a loop to perform this action, but you will notice that pairing a lambda and map make this very condensed.
example_list = [1, 2, 3, 4, 5, 6, 7, 8]
example_list = list(filter(lambda x: x % 4 == 0, example_list))
print(example_list)
output => [4, 8]
Above we have another example of how to use a lambda with an iterable. Again, we start with the same list, and we are editing it using the filter function. Similarly to the map function, this takes a condition and an iterable, and will return an object containing all elements that were true for the condition. In this case, we want a list of all the elements that are divisible by 4. Now, we could use a separately defined function again, but a lambda makes our code much more concise.
Thank you!
If you enjoyed reading or learned something, let me know below! Thank you for taking the time to check out my guide. Cheers!
Posted on April 22, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.