IntroductionYou've worked with both functions and methods, but there's a different type of function, the anonymous function. Anonymous functions are just functions without names, and in Python, they are handled with the lambda operator.
Anonymous functions allow you to use the behavior of a function without having to create a function. This is especially good when you only need to use a function once or twice. It might sound like an unusual occurrence, but it happens fairly frequently when a function or method takes a function as a parameter.
LambdasThe Lambda operator has a few basic parts. It consists of
lambda, a list of parameters, and an operation. The operation is separated from the other two with a colon. Check out this example.
foo = lambda a=2, b=1 : a / b
If you're using lambdas in a standalone capacity, you can set them equal to a variable. In this form, they aren't that versatile, but they can still play a role in creating functions on the fly.
Lambdas really find their use in being passed to other functions. See how it works in the example below.
def do_something(function, list_a, list_b):
for x in list_a:
for y in list_b:
do_something(lambda x, y : print(x*y), [1, 3, 5], [2, 4, 6])
Of course, this example isn't all that practical. It does illustrate the point, though. You can create a lambda within a function call to pass it as an argument to another function.
map() function is an interesting one. It allows you to iterate simultaneously over one or more lists and pass them to a function. It's very easy for that function to be a lambda expression.
This example can help clarify.
print(list(map(lambda x, y : x * y, [1, 2, 3, 4, 5], [2, 4, 6, 8, 10])))
That looks like a mess, so it's best to break it down. The main chunk starts with
map. It takes at least two arguments, a function and a list. It can take multiple lists, though, and this time there are two. It also takes a lambda expression that accepts two arguments and multiplies them together.
The map function runs through those lists. It sets each element in the first list in the first argument of the function and each element in the second list in the second function argument. It runs the function each time. The
map() function returns a map object.
That map object needs to be converted into a more usable form. The
list() function turns the object into a regular list. Then,
print() prints out that list.
Take a look at another one.
print(list(map(lambda x : x*2, [1, 2, 3, 4, 5])))
The simpler example might be easier to follow.
filter() function is another function similar to
map(). It takes a function and a list as arguments and iterates over the list, running the elements through the function.
Instead of just running a function,
filter() uses the function as a test condition for the elements in the list. The object that it creates contains all of the elements where the condition evaluated "True."
print(list(filter(lambda x : x > 5, [2, 4, 6, 8])))
The resulting list only contained 6 and 8 because those were the two conditions where the condition given in the function evaluated "True."
filter(), when used in conjunction with a lambda expression, is an efficient way to sort through and test lists against a condition.
Closing ThoughtsLambdas aren't the most popular topic in Python. Many developers don't even like them. Even Python's creator isn't a fan.
If you happen to like them, great. If not, don't worry. There are alternatives that will be discussed in later guides.
For now, it's just important to be aware of lambdas and how they work. You will encounter them from time to time, and they can be the easiest solution.
- Create a lambda expression that multiplies two numbers and set it equal to a variable.
- Use that variable to call the lambda and pass it two numbers.
- Write a function that takes a function as a parameter. Call that function with and pass it a lambda.
- Use the
map() function along with a lambda to add each element in two lists.
- Use the
filter() function along with a lambda to test if each element in a list is odd.