Why doesn't print work in a lambda?
A lambda
's body has to be a single expression. In Python 2.x, print
is a statement. However, in Python 3, print
is a function (and a function application is an expression, so it will work in a lambda). You can (and should, for forward compatibility :) use the back-ported print function if you are using the latest Python 2.x:
In [1324]: from __future__ import print_function
In [1325]: f = lambda x: print(x)
In [1326]: f("HI")
HI
Lambda SNS Not Printing Message
You are not returning anything from your lambda_handler
, thus the output is just None
. You have to add return
statement(s) to your code with actual return values that you want to output from the lambda.
Print statement inside function used within lambda function not executing
Works:
def gen_window(xi, n):
x, i = xi
l = []
for offset in range(n):
print("-->", (i - offset, (i, x)))
l.append((i - offset, (i, x)))
return l
xi = [3,5]
n = 3
gen_window(xi, n)
Lambdas only get executed wenn you actually use them - If you get no output you are probably never using it.
Output:
--> (5, (5, 3))
--> (4, (5, 3))
--> (3, (5, 3))
Use print inside lambda
You can import print_function
from the __future__
and use it as a function like this
from __future__ import print_function
map(print, [1, 2, 3])
# 1
# 2
# 3
Why can't we directly print the output of a lambda function/expression directly as a string,list, or tuple?
Your problem has nothing to do with lambda
, if you used a function you would run into the same problem - map
returns an object, which is an
iterable, ie. it can be converted to a list of values, but it is not a list of values. To print such a list, you must explicitly require its creation:
print(list(map(lambda x: (float(9)/5)*x + 32, Cel)))
Is there a way to perform if in python's lambda?
The syntax you're looking for:
lambda x: True if x % 2 == 0 else False
But you can't use print
or raise
in a lambda.
Related Topics
Types That Define '_Eq_' Are Unhashable
Equivalent of Numpy.Argsort() in Basic Python
How to Bind the Enter Key to a Function in Tkinter
Remove Reverse Duplicates from Dataframe
Generating File to Download with Django
Pandas - Explanation on Apply Function Being Slow
Is It Pythonic to Import Inside Functions
How to Escape Latex Code Received Through User Input
Most Pythonic Way to Interleave Two Strings
How to Get Rid of Beautifulsoup User Warning
How Could I Use Batch Normalization in Tensorflow
Datetime from String in Python, Best-Guessing String Format
Opencv Python Rotate Image by X Degrees Around Specific Point
How to Remove the Top and Right Axis in Matplotlib
Deleting Multiple Columns Based on Column Names in Pandas