[Python] Make the function a lambda function

Premise

Returns "hoge" when 0 comes in as an argument A function that just returns the input value when anything else comes. I also wrote the detailed procedure below.

As I often get lost, it is a memorandum.

Conclusion

def x(value):
    if value == 0:
        return "hoge"
    else:
        return value

This is

y = lambda value : "hoge" if value == 0 else value

It will be like this.

Expressed in letters:

(Function name) = lambda (argument) : (Value to return if the condition is True) if (conditions) else (conditionsがFalseなら返す値)

If you look at this at first glance, you will not understand it.

procedure

The lambda function is Because the function you want to change must be a one-line function First, put the following if statement on one line.

#Reprint
def x(value):
    if value == 0:
        return "hoge"
    else:
        return value

(1) First, describe the conditions for becoming True. (if value == 0)

def x(value):
    if value == 0

(2) When it becomes True, write the value you want to return in [Start]. ("hoge")

def x(value):
    "hoge" if value == 0

③ Enter the value to be returned when False (else value)

def x(value):
    "hoge" if value == 0 else value

This succeeded in making the if statement into one sentence. Then put the function on one line.

④ Erase unnecessary parts.

↓ 3 things you don't need 1, def 2, function name → x 3, parentheses surrounding the argument → ()

value :
    "hoge" if value == 0 else value

When the lines are aligned

value : "hoge" if value == 0 else value

You can do this. It's getting closer and closer.

⑤ Write "lambda" at the beginning and substitute it for the value.

Let's make it a lambda function. Write lambda at the beginning

lambda value : "hoge" if value == 0 else value

Substitute for x. (x is the function name)

x = lambda value : "hoge" if value == 0 else value

That's all there is to it.

Run

x(0)
>>> 'hoge'

x(100)
>>> 100

that's all.

Recommended Posts

[Python] Make the function a lambda function
[Python] Make sure the received function is a user-defined function
A python lambda expression ...
Make a function decorator
Get the caller of a function in Python
Make a copy of the list in Python
Create a function in Python
About the enumerate function (python)
Make a bookmarklet in Python
Make a fortune with Python
[Practice] Make a Watson app with Python! # 2 [Translation function]
Make a breakpoint on the c layer with python
What does the last () in a function mean in Python?
[Python] Smasher tried to make the video loading process a function using a generator
[Python] What is a zip function?
Call a Python function from p5.js.
python function ①
Let's make a GUI with python.
Associate Python Enum with a function and make it Callable
[Python] function
Write AWS Lambda function in Python
How to make a recursive function
python / Make a dict from a list.
Make a recommender system with python
[Lambda] Make import requests available [python]
Let's make a graph with python! !!
python function ②
Check the argument type annotation when executing a function in Python and make an error
[Introduction to Python] How to split a character string with the split function
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
[Python3] Define a decorator to measure the execution time of a function
Slack --APIGateway --Lambda (Python) --How to make a RedShift interactive app
[Python] A simple function to find the center coordinates of a circle
Make the Python console covered with UNKO
Use print in a Python2 lambda expression
Let's make a shiritori game with Python
Precautions when pickling a function in python
[Python] How to make a class iterable
Write the test in a python docstring
OR the List in Python (zip function)
Make a relation diagram of Python module
Search the maze with the python A * algorithm
[Python3] Rewrite the code object of the function
Run the Python interpreter in a script
Let's make a voice slowly with Python
Make Qt for Python app a desktop app
[python] [meta] Is the type of python a type?
Lambda function to take AMI backup (python)
Let's make a web framework with Python! (1)
Let's make a combination calculation in Python
The story of blackjack A processing (python)
Make a desktop app with Python with Electron
[Python] A progress bar on the terminal
Let's make a Twitter Bot with Python!
[Python] A program that rounds the score
Let's make a web framework with Python! (2)
"Cython" tutorial to make Python explosive: When a function on the C ++ side has an overload.
python3x: lambda function
python enumerate function
[Python] Explains how to use the range function with a concrete example
Python> function> Closure