Understanding Lambda Functions in Python: Definition, Usage, and Applications



This content originally appeared on DEV Community and was authored by Pri

What is a lambda Function?

In simple terms, a lambda function is a small anonymous function, meaning it is defined without a name. A lambda function can take any amount of arguments; however, it is only able to have one expression.

lambda functions are typically used inside of other functions like map(), filter(), or sort(). It does not need to be assigned to a variable, so it behaves like a function that is defined with def. It is important to keep in mind that it does not replace def, as lambda functions are limited to a single expression.`

How Do We Use lambda Functions?

Syntax:

lambda syntax

Key Points:
  • lambda is the keyword used to define the function.
  • It can take any number of arguments.
  • No return statement needed because it evaluates and returns the value of the expression automatically.
  • Usually used for simple functions.
  • It is like a “throwaway” function. If writing a complete function is unnecessary and you only need it temporarily, try using a lambda function!
Example:

If you were to square a number using a regular function, you would write something like this:

regular python function example

The lambda function equivalent is this:

lambda function example

How Are lambda Functions Useful?

lambda functions show their practicality when used inside of another function.

If you have a function definition taking in one argument and that argument is multiplied by a number that is unknown, it would look something like this:

lambda function inside of another function

You are now able to use this function definition to create a function that always doubles… or triples the number you input:

lambda function doubling and tripling

Using a lambda Function With filter(), map(), and sort()

filter() – used to filter specific values from a set of data.

example using lambda with filter

filter(function, iterable) only keeps the elements for which the function returns True. The lambda x: x % 2 == 0 checks if the numbers are even, one-by-one. filter() applies the lambda to every element of numbers (the list of integers [1-10]) and keeps only the ones that return True (the even ones). filter() returns an iterator, so we wrap it in list() to see the filtered results.

map() – applies a function to every element.

example using lambda with map

map(function, iterable) applies the function to every element of the numbers list. The lambda x: x * 2 doubles each number on that list.

sort() With key – sorting logic that is custom.

example using lambda with sort

The sort(key=function) decides how to order the elements by using the function’s return value. The lambda x: len(x) sorts each fruit by length rather than by alphabetical order.

lambda Function Use Cases

In the real world, you may use lambda with something like filtering an email from a list, so if you were looking for only @gmail.com addresses, you would be able to keep the @gmail.com addresses.

You may use lambda when mapping through a list to calculate item prices with tax. That way, lambda would multiply each price to add a tax.

Lastly, you could use a lambda function to sort through people by age, that way your return is the age of each person and sort() would organize it from youngest to oldest.

How will you try using a lambda function?





References

BroCode. (2024, July 14). Learn Python LAMBDA in 6 minutes! 🚮. YouTube. https://www.youtube.com/watch?v=IljPHDyBRog

How are lambdas useful?. Stack Overflow. (2009, May 20). https://stackoverflow.com/questions/890128/how-are-lambdas-useful

Kumar, R. (2022, September 4). Powerful use-cases of lambda function in Python💪. Medium. https://medium.com/@ravikumar10593/powerful-use-cases-of-lambda-function-in-python-d4ccefe5f3d2

Reshef, G. (2021, December 8). What is the purpose of lambda expressions?. Discuss Python. https://discuss.python.org/t/what-is-the-purpose-of-lambda-expressions/12415

W3schools.com. W3Schools Online Web Tutorials. (n.d.). https://www.w3schools.com/python/python_lambda.asp


This content originally appeared on DEV Community and was authored by Pri