DEV Community

Cover image for Python Lambda Function
Baransel
Baransel

Posted on

Python Lambda Function

Sign up to my newsletter!.

Why Lambda Function?

In our previous lessons, we discussed Functions, if we describe functions with a sentence, we used functions to avoid code repetition, to write more organized code and most importantly to get rid of code complexity. If you haven't looked at the Functions lesson, you should click on the link and look at that lesson first in order to better understand this lesson.

Lambda function is an advanced function, like recursive functions. Well, if you ask what lambda functions will give us. There are many answers to this; it has many advantages like writing less code, more organized code and so on. Yes, you write much less code with lambda functions than with normal functions, because lambda functions are single-line functions. In this lesson, you will learn how to write a function with only one line.

Well, do you have to use Lambda functions, of course not, but you will write less code with Lambda functions and your codes will be more organized. For this reason, you will be faster and more practical with the Lambda functions.

You can't use lambda functions to write very complex functions, you can only use them to practice small functions.

How to Use Lambda Function?

Before explaining lambda functions, let's create a normal function first and then create the same function with a lambda function, then you will understand the difference much better.

Let's write a function that calculates the square of numbers;

def getSquare(number):
    return number**2

print(getSquare(5))
Enter fullscreen mode Exit fullscreen mode

Now let's write the same function as a lambda function, let's see a general lambda usage outline before that.

FunctionName = lambda parameter1,parameter2: return value

Now let's write the lambda function that takes the square of the entered number.

getSquare = lambda x:x**2

print(getSquare(5))
Enter fullscreen mode Exit fullscreen mode

Lambda functions are very similar to List Comprehensions, let's show them with an example.

list1 = [1,2,3,4,5,6,7,8,9,10]

Now, let's create the square of all the elements of the list named list1 and create the list namedlist2 with list comprehension.

list2 = [i**2 for i in list1]
print(list2)

# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Enter fullscreen mode Exit fullscreen mode

As you can see, they have very similar uses.

Maybe it may seem meaningless or unnecessary to you now, but you will see that it is much more comfortable to use the lambda function with the embedded functions that we will cover in our next lesson. Also, at higher levels Python's Pandas etc. You will encounter this type of structure frequently when dealing with libraries.

As I mentioned before, you can't use it in very complex functions, you can only use it in small functions for less code and practice.

You might also like

Python Functions
Python File Operations
Python Set and Frozenset
Python dictionary and dictionary methods
Python tuple and tuple methods

Top comments (0)