Python Lambda function

The Python Lambda is well known by most programmers as being a way of quick way to write a small, one-line function. However it’s uses go beyond just this. In this article we’ll cover the Python Lambda function and it’s uses, complete with examples.

Lambda Functions are also known as anonymous functions. Further more, while there is no limit to the number of arguments a Lambda can take, there can only be a single expression written on a single line.


Python Lambda

Below is a simple example of a lambda function. The : character separates the arguments from the expression. The lambda keyword precedes everything and defines it as a lambda function.

inc = lambda a: a + 1
print(inc(1))
print(inc(5))
2
6

The regular equivalent to this is shown below.

def inc(a):
    return a + 1

print(inc(1))
print(inc(5))

Using multiple arguments

You can create multiple arguments for a lambda function by separately unknowns with a comma. See the example below.

add = lambda x,y : x + y 
print(add(2,4))

Anonymous Functions

Now that we’ve covered the basic Lambda usage, we’ll move on to it’s use as an anonymous function in another function. The premise of this revolves around the function returning the lambda.

In a way, the function func is acting as a lambda function creator. We use this to create 2 lambda functions which calculate the square and cube of a number respectively.

def func(n):
    return lambda x : x ** n

sqr = func(2)
cube = func(3)

print(sqr(3))
print(cube(3))
9
27

It might take a while to understand this. Copy the code and experiment with it on your own.


When to use the Lambda Function?

Just so we’re clear, you should know when to be using the Python lambda, and when to be using regular functions. The lambda function is used with the primary goal of creating a temporary function. It’s temporary because unlike regular functions you can’t call the same thing over and over again. It’s not bound to an identifier after all.

Lambda’s primary goal is to be reducing the amount of code you write and making your code look simpler. It’s also slightly faster than a regular function. The following stack-overflow answer has some examples regarding this if you’re interested.

Finally we’re including some examples of the Python Lambda function paired with several built in functions to demonstrate it’s use further.


Lambda with map()

If you’re familiar with this function, you’ll know it has the ability to perform a given function on an iterable. Normally we would have to create a whole separate function to reference, but with lambda we can write it straight away in one line.

list1 = [1,3,8,4,2,5] 
result = list(map(lambda x: x + 1 , list1)) 
print(result) 

Lambda with filter()

As the name implies, this function filters through the elements of an iterable and returns those elements which meet a certain criteria. This criteria is determined by the function which is passed as the first parameter.

list1 = [3,5,4,8,6,33,22,18,76,1] 
result = list(filter(lambda x: (x%2 != 0) , list1)) 
print(result) 

The function above returns True if the number is odd and False if it is even. Hence it returns all odd numbers.

[3, 5, 33, 1]

This marks the end of the Python Lambda Function Article. Any suggestions or contributions for CodersLegacy are more than welcome. Relevant questions can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments