0
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 function, it is working fine. But I am trying to add printing code also in the same statement.

Following code worked for me,

map(lambda i: print(i),["%d even"% i if i % 2 == 0 else "%d odd"% i for i in random.sample(range(100), 10)])
user1423015
  • 593
  • 1
  • 4
  • 12
  • 2
    Are you calling the lambda anywhere? defining lambda would not automatically call it. – Anand S Kumar Jul 21 '15 at 14:31
  • @Anand, thanks. would you please let me know how to call that lambda here – user1423015 Jul 21 '15 at 14:34
  • What are you really trying to accomplish? Are you trying to call the print function from within the list comprehension? – Bryan Oakley Jul 21 '15 at 14:40
  • What do you mean "it is working fine"? What are you actually trying to accomplish? Show us how you're using this other function. It would also help to know if you're using python 2.x or 3.x, since `print` is a function in 3.x but not 2.x. – Bryan Oakley Jul 21 '15 at 14:42
  • from __future__ import print_function l=lambda : [(print("%d even"% i),i) if i % 2 == 0 else (print( "%d odd"% i),i) for i in random.sample(range(100), 10)] l() – Kristian Damian Jul 21 '15 at 14:57

3 Answers3

0

The comma is terminating the lambda. You are creating a tuple with two values. The first is lambda i: print(i), and the second is the list comprehension. It is as if you're doing this:

def func(i):
    print(i)
(func, ["%d...])

The second problem is that you don't seem to be calling the lambda anywhere. lambda simply creates a function that doesn't have a name. For it to execute, you need to assign it to a variable, and then call it:

x = lambda i: print(i)
x(12)

If what you're trying to do is call the print function from within the list comprehension, it should work fine in python 3.x since print is a function. If you are trying to get this to work in python 2.x, there are a couple solutions. For one, you can import the print function:

from __future__ import print_function
[print("%d even"% i) if i % 2 == 0 else print("%d odd"% i) for i in random.sample(range(100), 10)]

If you want to use lambda, then save the lambda as a variable, and use the variable in the list comprehension:

func = lambda i: print(i)
[func("%d even"% i) if i % 2 == 0 else func("%d odd"% i) for i in random.sample(range(100), 10)]
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • removing comma is also not working. but if i remove the lambda part and use another function to print values it is working. But I am trying to add print function also in single statement. Then , it is not working. – user1423015 Jul 21 '15 at 14:38
0

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

From Why doesn't print work in a lambda?

If you are using python2.x. Try this first

from __future__ import print_function

Example

In [2]: from __future__ import print_function

In [3]: myfn = lambda i: print(i)

In [4]: myfn(55)
55
Community
  • 1
  • 1
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
0

You are making a tuple composed by a lambda function and a list.

However, you can easily access to the last item of your tuple (that is the result you want to print):

>>> g = lambda i: print(i),["%d even"% i if i % 2 == 0 else "%d odd"% i for i in random.sample(range(100), 10)]

>>> g
(<function <lambda> at 0x1063fa268>, ['52 even', '9 odd', '25 odd', '57 odd', '77 odd', '71 odd', '63 odd', '99 odd', '8 even', '79 odd'])

>>> type(g)
<class 'tuple'>

>>> for item in g:
...  print(item)
... 
<function <lambda> at 0x1063fa268>
['52 even', '9 odd', '25 odd', '57 odd', '77 odd', '71 odd', '63 odd', '99 odd', '8 even', '79 odd']

>>> g[0]
<function <lambda> at 0x1063fa268>

>>> g[1]
['52 even', '9 odd', '25 odd', '57 odd', '77 odd', '71 odd', '63 odd', '99 odd', '8 even', '79 odd']
alec_djinn
  • 10,104
  • 8
  • 46
  • 71