search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
near_me
Linear Algebra
52 guides
keyboard_arrow_down
chevron_leftFunctions
check_circle
Mark as learned
thumb_up
2
thumb_down
0
auto_stories Bi-column layout
settings

# Lambda functions in Python

schedule May 21, 2023
Last updated
local_offer
Python
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Python's lambda functions are a concise way to return a value from a single expression. They are often used within other functions (Python supports functional programming) and can even be set as the return value.

The general syntax for a lambda function:

``` lambda arguments: expression ```

A very basic lambda function that squares `x` adds one and returns the result:

``` lambda x: x**2 + 1 ```

# Examples

## Multiple arguments

To square `x` and `y` and return their sum:

``` a = lambda x,y: x**2 + y**2a(1,2) 5 ```

## Input to other functions

To specify a lambda function as an input to `map` function:

``` nums = map(lambda x: x**2 + 1, [1, 2, 3])print(*nums) 2 5 10 ```

## As return value

To use lambda function as the return value:

``` def sqrd_plus_n(n): return lambda x: x**2 + na = sqrd_plus_n(3) #We set n as 3 (adding 3)a(2) #2**2 + 3 7 ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!