Python Lambda with underscore as an argument

In Python, we use the lambda keyword to declare an anonymous function. Lambda function behaves in the same way as regular functions behave that are declared using the ‘def’ keyword. The following are some of the characteristics of Python lambda functions:

  • A lambda function can take more than one number of arguments, but they contain only a single expression.
  • Lambda functions used to return function objects.
  • Syntactically, lambda functions are restricted to only one single expression.

Note: For more information, refer Python lambda

Creating a Lambda Function

Lambda functions can be created using the lambda keyword. We use the given syntax to declare a lambda function:

lambda argument(s) : expression

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

remainder = lambda num: num % 2
  
print(remainder(5))

chevron_right


Output:



1

Python Lambda with underscore

The ‘_’ is the variable name. This variable name is usually a name for an ignored variable.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

l = lambda _: True
  
l(1)

chevron_right


Output :

True

This function can be used when we want to get a specific output for every input.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.