Linked Questions
16 questions linked to/from Why doesn't print work in a lambda?
1 vote
2 answers
5k views
invalid syntax on print statement python 2.7 [duplicate]
I have some code to test other codes that I've written(in ipython notebook). print_closest = lambda w, wl: print('{}: {} ({})'.format(w, *closest_match(w, wl))) This is the code I have and it works ...
2 votes
0 answers
7k views
How to write lambda with no return and no arguments [duplicate]
I want to pass a function/lambda to a method and then call it in python 2.7: def foo(f): f() I tried many ways but didnt find the correct syntax. For example this foo(lambda: print("hallo")) ...
529 votes
16 answers
904k views
Is there a way to perform "if" in python's lambda? [duplicate]
In Python 2.6, I want to do: f = lambda x: if x==2 print x else raise Exception() f(2) #should print "2" f(3) #should throw an exception This clearly isn't the syntax. Is it possible to ...
167 votes
22 answers
263k views
Is it possible to have multiple statements in a python lambda expression? [duplicate]
I have a list of lists: lst = [[567, 345, 234], [253, 465, 756, 2345], [333, 777, 111, 555]] I want map lst into another list containing only the second smallest number from each sublist. So the ...
18 votes
6 answers
11k views
Why map(print, a_list) doesn't work?
For a normal function, map works well: def increment(n): return n+1 l = [1, 2, 3, 4, 5] l = map(increment, l) print l >>> [2, 3, 4, 5, 6] However, if it's print being put inside the map ...
16 votes
2 answers
30k views
Is it possible to use an inline function in a Thread call?
The documentation for threading.Thread(target=...) states that target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called. I usually use it like ...
11 votes
3 answers
30k views
Difference between RDD.foreach() and RDD.map()
I am learning Spark in Python and wondering can anyone explain the difference between the action foreach() and transformation map()? rdd.map() returns a new RDD, like the original map function in ...
3 votes
2 answers
4k views
Can Monkey patching replace existing function definition in a class?
I know how fierce the SO community is so I'll try my best to keep the question minimal, complete and verifiable. What I simply want to know is can monkey patching be used to replace the definition of ...
4 votes
2 answers
4k views
Lambda invalid syntax
This: add = lambda x, y: x += y Gives: SyntaxError: invalid syntax My task is to be able to mullitply or add every number between 1-513 with 1 function and 2 lambda functions. So if you have any ...
2 votes
3 answers
1k views
Python: Aliasing an If-Else conditional statement?
Lets say we have the following: my_condition = True print('AAA' if my_condition else 'BBB') The output would be: AAA Conversely, if my_condition became False: my_condition = False print('AAA' if ...
3 votes
1 answer
1k views
KivyMD | List Item print its "text" on_press
How can I code a OneLineListItem in KivyMD to print its "text" on a console? I came up with something like this: [...] item = OneLineListItem(text="DEMO", on_press= lambda x: ??????...
0 votes
2 answers
631 views
loop through matrix using map() function in python
how to use map() instead of the following nested for loop the idea to not use for loop :) def f(matrix): r, c = np.shape(array) for col in range(0,c): for row in range(0,r): ...
0 votes
1 answer
531 views
how to print the letter if it exists in the list using lambda,filter function
letters = ['a', 'b', 'c', 'd', 'e', 'f'] list(filter(lambda x : print(x) if 'e' in x ,letters)) SyntaxError: invalid syntax
0 votes
3 answers
420 views
lambda with print and list comprehension
lambda i: print(i),["%d even"% i if i % 2 == 0 else "%d odd"% i for i in random.sample(range(100), 10)] What is wrong with this code, it is not printing anything. If I try to print by using another ...
-1 votes
1 answer
197 views
List of Python keywords that are valid within (lambda) expression
For code completion with Scintilla.Net i need the list of keywords that are valid in an expression. You can get all keywords via the "keyword" module, but for example "raise" and "print" cannot be ...