Eager evaluation of lambdas within a list comprehension

if else in list comprehension
nested list comprehension python
python any list comprehension
python lambda
python list comprehension multiple conditions
advanced list comprehension python
python list comprehension multiple variables
python lambda function list comprehension

Consider a python function:

def testSetsFromStrings(tt):
  x1= [lambda x: x.split(' ') for t in tt]
  return x1

Let's invoke it with the following :

tt = ['id0 id1 id2 id3 id4', 
'id10 id11 id12 id13 id14', 
'id20 id21 id22 id23 id24', 
' id30 id31 id32 id33 id34', 
'id50 id51 id52 id53 id54']


A breakpoint was placed after the x1= .. line and we can see x1 =

<class 'list'>: [<function testSetsFromStrings.<locals>.<listcomp>.
<lambda> at 0x11cee5730>, <function testSetsFromStrings.<locals>.<listcomp>.
<lambda> at 0x11cee5840>, <function testSetsFromStrings.<locals>.<listcomp>.
<lambda> at 0x11cee58c8>, <function testSetsFromStrings.<locals>.<listcomp>.
<lambda> at 0x11cee5950>, <function testSetsFromStrings.<locals>.<listcomp>.
<lambda> at 0x11cee59d8>]

I am at a loss as to how to cause that lambda to be eagerly evaluated. What can be done here?

** Update**

The logic shown is a simplification of the multi step function that is needed: to focus on just the mechanics of invoking a lambda. Replacing the lambda with directly invoking split does not address the real need.

Perhaps you don't want lambdas at all? Is this what you want?

def testSetsFromStrings(tt):
    x1 = [x.split(' ') for x in tt]
    return x1

Lambdas are functions: they are evaluated when you call them. If you want them to run immediately, then you probably don't need a lambda at all.

If you need to invoke a function, then invoke it:

def testSetsFromStrings(tt):
    x1 = [my_function(x) for x in tt]
    return x1

Proceedings of the 1996 ACM SIGPLAN International Conference on , Id ( 19 ) is an implicitly parallel , mostly functional , language designed in the algebraic types , pattern - matching notation , array and list comprehensions , etc . The main novelty of Id is its implicitly parallel evaluation model : everything is evaluated eagerly , except for expressions inside conditionals and inside lambdas . With the list comprehension above, we read each number from a list containing 2 numbers from a list containing 5 elements. Then we apply an ‘if’ statement to filter out all the odd numbers.

You're defining the function, but not applying any arguments to it:

Try this instead

x1 = [(lambda x: x.split(' '))(x) for x in tt]

But it would be better to just extract the function definition outside of the comprehension if the function is at all complicated and then use a map or list comprehension.

Python Quirks: Lambdas, Python lambdas are weird and underpowered. speed improvements when using list comprehensions, which are eagerly evaluated. This tutorial is all about Python Lambda Function List Comprehension. I will also demonstrate the use of lambda function with map(), filter() and reduce(). At the end of this tutorial, you will understand every basic practical detail you should know to use the Lambda function in your Python code.

You could also use a map function instead of a list comprehension:

x1 = map(lambda x: x.split(' '), tt)

if you prefer the lamda function being present. Elsewhere just:

x1 = [x.split(' ') for x in tt]

as mentioned from others as well.

Systems and Computer Technology: Proceedings of the 2014 , The usual implementation strategy for lazy evaluation in functional programming Lambda calculus (also written as λ-calculus) is a formal system in user-​defined data types, pattern matching, and list comprehensions (Hudak et al., 2007). you can use a lambda function in a Python list comprehension as follows: >>> [(lambda x:x*x)(x) for x in range(1,4)] [1, 4, 9] But there is no point in doing so when the comprehension essentially gives you the lambda for free (you simply write the expression that would have been in the body of the lambda: >>> [x*x for x in range(1,4)] [1, 4, 9]

What you need to understand is that lambdas are inherently functions themselves. Therefore, create a lambda function and invoke that.

testSetsFromStrings = lambda x: x.split(' ')
x1 = [testSetsFromStrings(t) for t in tt]

Python Basics: List Comprehensions, List comprehensions provide us with a simple way to create a list based on some iterable. Mostly, the lambda functions are passed as parameters to functions which expect a function So, if we place a list comprehension in the output expression of another list They use the lazy evaluation technique. The lambda syntax is fairly simple. But it might take a bit of time to get used to. You declare a lambda function with the word lambda followed by a list of arguments, followed by a colon and then a single expression and this is key. There's only one expression to be evaluated in a lambda. The expression value is returned on execution of the lambda.

Lambda vs. List Comprehension - The Startup, Which can be especially useful when you have an abundant amount of loops within your code. Beginners in Python or coding in general are not  If the input list can grow large then there is a worse problem: you're iterating over the whole list, while you could stop at the first element. You could accomplish this with a for loop, but if you want to use a comprehension-like statement, here come generator expressions :

Python for Data Analysis, 219 pymongo driver, 175 pyplot module, 220 pystatsmodels (mailing list), 12 Python 395–396 str data type, 396–398 type casting in, 399 dict comprehensions in, 425–426 currying of, 427 extended call syntax for, 426 lambda functions, 424 393 references in, 389–390 strict evaluation, 394 strongly-typed language,  Generally speaking, I think a list comprehension is better. If you are using a lambda, it means that the operation is a simple expression. In those cases, using map results in a function call for each element, while a list comprehension ends up being compiled into a simple loop that builds the list.

Python and lazy evaluation - A geek with a hat, I will write a post about using them for lazy evaluation! 0 for p in takewhile(​lambda x: n>x, primes())): yield n print take(10, primes()) bad – using generator comprehensions instead of list comprehensions is a great idea. Lambda expressions (C# Programming Guide) 07/29/2019; 11 minutes to read +8; In this article. A lambda expression is an expression of any of the following two forms:. Expression lambda that has an expression as its body:

  • What is the expected behavior?
  • this should work without a function or unnecessary lambda [x.split(' ') for x in tt]
  • If you think about that comprehension (for each x in tt, return a new lambda...) it should make sense to you why you are seeing what you are seeing...
  • @dawg Maybe so but I'm not sure how to structure this to get the result of evaluting a lambda or function.
  • The standalone lambda x: x.split(' ') only defines a lambda function; it does not call the lambda function. Either use map to define a callable and call it in one step or defined the function or lambda externally from the list comprehension.
  • Well I had a function in place of the lambda earlier .. I want to be able to invoke either a function or a lambda. The logic shown is a much simpler representation of what I want to do - to just ask about how to invoke a lambda or function.
  • You'll need to explain your full situation then.
  • Updated the OP. If necessary i'll convert the lambda to a multi-step function which includes statements (such as print) to illustrate that I really do need the original question answered.
  • I've updated my answer: invoke a function if you need to.
  • ok - so a lambda simply does not make sense/work here; it's either a direct application of the logic or invoke a function. That's an acceptable explanation. I'll accept this answer after the requisite few minutes.