[PYTHON] I came across a lambda expression when I was worried about functionalization

1️⃣ Overview

I often wondered, "Should the code I'm writing now be a function, or should I write it directly in the main: thinking:", but at that time I came across a lambda expression and that was It's very easy to handle, and I was able to put it together compactly when I looked over the entire code, so I'll write an article with a memorandum. : writing_hand:

2️⃣ About lambda expression

I would like to explain the lambda expression with code. Also, this time, I will deal with addition as a simple example, but I would like to explain how to write practical code in the next section.

python


def hoge(num1: int, num2: int):
    num = num1 + num2
    return num

Like this, even a function that does simple addition needs to add `` `return``` at the end. Also, when executing the above function,

python


print(hoge(2, 6))
# 8

In order to confirm that the function is processed correctly, I think that it will be confirmed by inserting a `print statement`. However, if you haven't done any complicated processing to make it a function, or if you don't want to use that function elsewhere, you may want to write it a little more compactly. You can use a lambda expression in such a case! The above addition is described by a lambda expression as follows.

python


print((lambda num1, num2:num1 + num2)(2, 6))
# 8

Explanation of lambda expression: (lambda argument name: argument processing) (value required for calculation) With just such a description, it was very easy to read and I was able to code it compactly: blush: It is also possible to make a lambda expression with arguments by assigning it to a variable.

python


hoge = lambda num1, num2:num1 + num2
print(hoge(2, 6))
# 8

Also, personally speaking, it's kind of cool to be able to code in a lambda expression: smile: (It looks like someone who can do it ...)

3️⃣ What is practical lambda coding?

If you keep the explanation of the above item, it seems that it will be "It does not change much even if you write it with a function !: grimacing:", so I would like to think about how to demonstrate the true value of the lambda expression here. I will!

For example ...

python


hoge_list = [3, -5, 10, 6, -2, -1]

If you remove negative values from this list and want to sort them in ascending order, use a lambda expression ...

python


print(sorted(filter(lambda num_plus: num_plus >= 0, hoge_list)))
# [3, 6, 10]

With the feeling like, the first `sorted function ``` executes the process of arranging in ascending order, and the `filter function ``` describes the condition like an if statement, so it is very easy. Can be described in! : tada:

Summary

By using a lambda expression, you don't have to bother to write for statements and if statements, so you can express the code in a compact and easy-to-read manner, and it seems to be useful even if you hesitate to make it a function. , I would like to use it more and more when I encounter a situation where I can use the lambda expression! : muscle:

Recommended Posts

I came across a lambda expression when I was worried about functionalization
What I was worried about when displaying images with matplotlib
Where I was worried about heroku
[Linux] Let's talk about when I stumbled upon a symbolic link I was using.
A python lambda expression ...
What I was careful about when implementing Airflow with docker-compose
A story that I was addicted to calling Lambda from AWS Lambda.
A story that I fixed when I got Lambda logs from Cloudwatch Logs
I came across an image filter with a clearly Japanese name called Kuwahara filter, and when I tried it, it was amazing, so I will introduce it.
A note I was addicted to when making a beep on Linux
A note I was addicted to when creating a table with SQLAlchemy
I have a question about whitespace
A story I was addicted to when inserting from Python to a PostgreSQL table
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.