Because return is a statement. Lambdas can only contain expressions.

Answer from Daniel Roseman on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lambda.asp
Python Lambda
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. ... The power of lambda is better shown when you use them as an anonymous function inside another function. Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number: ... def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11)) Try it Yourself ยป
๐ŸŒ
Amazon Web Services
docs.aws.amazon.com โ€บ aws lambda โ€บ developer guide โ€บ building lambda functions with python โ€บ define lambda function handler in python
Define Lambda function handler in Python - AWS Lambda
January 31, 2026 - If you use the RequestResponse invocation type to invoke a Lambda function synchronously, Lambda returns the result of the Python function call to the client invoking the Lambda function (in the HTTP response to the invocation request, serialized into JSON).
๐ŸŒ
Real Python
realpython.com โ€บ python-lambda
How to Use Python Lambda Functions โ€“ Real Python
December 1, 2023 - In Python, an anonymous function is created with the lambda keyword. More loosely, it may or not be assigned a name. Consider a two-argument anonymous function defined with lambda but not bound to a variable. The lambda is not given a name: ... The function above defines a lambda expression that takes two arguments and returns their sum.
๐ŸŒ
Dataquest
dataquest.io โ€บ blog โ€บ tutorial-lambda-functions-in-python
Tutorial: Lambda Functions in Python
March 6, 2023 - Usually, we pass a lambda function as an argument to a higher-order function (the one that takes in other functions as arguments), such as Python built-in functions like filter(), map(), or reduce(). Let's look at a simple example of a lambda function: ... The lambda function above takes a single argument, increments it by 1, and returns the result.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ operators โ€บ lambda.html
lambda โ€” Python Reference (The Right Way) 0.1 documentation
Both functions are the same. Note that lambda does not include a return statement. The right expression is the implicit return value.
Find elsewhere
๐ŸŒ
Afternerd
afternerd.com โ€บ blog โ€บ python-lambdas
Python Lambdas Explained (With Examples) - Afternerd
April 6, 2019 - A Python lambda is just another method to define a function. ... Lambda functions can accept zero or more arguments but only one expression. The return value of the lambda function is the value that this expression is evaluated to.
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-return-lambda-from-function
Python Return Lambda From Function โ€“ Be on the Right Side of Change
July 30, 2023 - In Python, you can return a lambda function from another function by declaring the lambda function inside the return statement of the outer function.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Lambda keyword (lambda): Defines an anonymous (inline) function in Python. Argument (x): The input value passed to the lambda function. Expression (x**2): The operation performed on the argument and returned as the result.
Published ย  2 weeks ago
Top answer
1 of 3
2
Decimal object is not JSON serializable. Use a package that can do that for you [1] or consider casting the Decimal into a float using a helper function [2]. 1. Use `simplejson` instead of `json` as it will do the decimal conversion for you. This would mean you need to use a Lambda layer to import that pip package. `import simplejson as json` 2. Use a custom class to do the conversion meaning you can avoid the Lambda layer: ``` from decimal import Decimal class DecimalEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, Decimal): return str(obj) return json.JSONEncoder.default(self, obj) ``` ``` json.dumps(item, cls=DecimalEncoder) ```
2 of 3
0
My code is def lambda_handler(event, context): dynamodb = boto3.resource("dynamodb") table = dynamodb.Table("OrderList") items = table.scan()['Items'] return { 'statusCode': 200, 'body': json.dumps(items[1]) } Response is { "statusCode": 200, "body": "{"TotalPrice": "101", "PartName": "Test Part", "ClientID": "user001", "UnitPrice": "50", "OrderDate": "28-10-2022", "PartID": "P003", "Status": "Sent", "Quantity": "1", "OrderID": "O0001"}" } ################################### return { 'statusCode': 200, 'body': json.dumps(items[1]) } Response is { "errorMessage": "Object of type Decimal is not JSON serializable", "errorType": "TypeError", "requestId": "5849b770-4dc6-4858-82ed-65fbecab1f6a", "stackTrace": [ " File "/var/task/lambda_function.py", line 76, in lambda_handler\n 'body': json.dumps(items)\n", " File "/var/lang/lib/python3.9/json/init.py", line 231, in dumps\n return _default_encoder.encode(obj)\n", " File "/var/lang/lib/python3.9/json/encoder.py", line 199, in encode\n chunks = self.iterencode(o, _one_shot=True)\n", " File "/var/lang/lib/python3.9/json/encoder.py", line 257, in iterencode\n return _iterencode(o, 0)\n", " File "/var/lang/lib/python3.9/json/encoder.py", line 179, in default\n raise TypeError(f'Object of type {o.class.name} '\n" ] }
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ python โ€บ how to use python lambda function with examples
How To Use Python Lambda Function With Examples
April 1, 2025 - This is why a lambda function canโ€™t support statements as it is an expression in itself. A lambda function doesnโ€™t support a return statement, but automatically returns the result of its expression.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-lambda-function-explained
How the Python Lambda Function Works โ€“ Explained with Examples
December 17, 2024 - As I explained above, the lambda function does not have a return keyword. As a result, it will return the result of the expression on its own. The x in it also serves as a placeholder for the value to be passed into the expression.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ python lambda function
Python Lambda Function | Towards Data Science
January 23, 2025 - Alonzo Church in the 1930s to the ... GitHub Repository below: ... The lambda function can take many arguments but can return only one expression....
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ python-lambda-functions-tutorial
How to use Python Lambda functions: a 5 minute tutorial
March 10, 2026 - In this Python tutorial, we will ... any number of arguments but only have one expression. Lambda functions return an object that is assigned to a variable or used as a part of other functions....
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ python lambda functions with examples
Python Lambda Functions with EXAMPLES
August 12, 2024 - Regular functions can have multiple ... as anonymous functions. Regular functions must have a name and signature. Lambdas do not contain a return statement because the body is automatically returned....
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ lambda-expressions
Python's lambda functions - Python Morsels
October 25, 2023 - The result of calling the single expression within a lambda expression will be the return value of the lambda function. So lambda functions can only execute a single Python expression and their return value will be the result of that single ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ controlflow.html
4. More Control Flow Tools โ€” Python 3.14.4 documentation
Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope: >>> def make_incrementor(n): ... return lambda x: x + n ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ lambda-functions-in-python
Lambda Functions in Python
May 16, 2023 - This creates an anonymous function that receives as input the variables x_1, ..., x_n and returns the evaluated expression(x_1, ..., x_n). โ€‹ The purpose of lambda functions is to be used as parameters for functions that accept functions as parameters, as we did with map() above. Python allows you to assign a lambda function to a variable, but the PEP 8 style guide advises against it.