48

Why is print(x) here not valid (SyntaxError) in the following list-comprehension?

my_list=[1,2,3]
[print(my_item) for my_item in my_list]

To contrast - the following doesn't give a syntax error:

def my_func(x):
    print(x)
[my_func(my_item) for my_item in my_list]
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
monojohnny
  • 5,894
  • 16
  • 59
  • 83

4 Answers4

61

Because print is not a function, it's a statement, and you can't have them in expressions. This gets more obvious if you use normal Python 2 syntax:

my_list=[1,2,3]
[print my_item for my_item in my_list]

That doesn't look quite right. :) The parenthesizes around my_item tricks you.

This has changed in Python 3, btw, where print is a function, where your code works just fine.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • 23
    You can import this feature from the future: `from __future__ import print_function` – Jochen Ritzel Jan 26 '10 at 17:16
  • @THC4k - I agree, this will make sure the code can be compiled in *both* 2.6 and 3.0 – Jason Coon Jan 26 '10 at 17:20
  • Right, since it specifically mentions 2.6, that's a good point. – Lennart Regebro Jan 26 '10 at 22:33
  • FWIW, in 2.7 and using the future import the above code doesn't work for me. a) I need to use brace syntax, `print(x)` to avoid syntax error msg, b) it emits a trailing list of Nones after printing the list items: https://gist.github.com/maphew/67c88807bf97bf67fbf9 – matt wilkie Apr 02 '15 at 20:44
  • 1
    @mattwilkie: Looking at your gist, that's the expected output. a) since `print` is now a function you need to use the brace syntax, b) you're returning a lists of `None`s since `print` doesn't return anything. – Lucas Godoy Jan 15 '16 at 19:18
  • @LucasGodoy the curious bit is the 2nd part, where brace is being used but print is printing out the list items as expected AND a series of Nones. – matt wilkie Jan 15 '16 at 19:30
  • ...and means the final "the above code works just fine" statement is wrong. It doesn't work -- cleanly that is. There's no syntax error but the results are likely unsatisfactory. – matt wilkie Jan 15 '16 at 23:22
9

list comprehension are designed to create a list. So using print inside it will give an error no-matter we use print() or print in 2.7 or 3.x. The code

[my_item for my_item in my_list] 

makes a new object of type list.

print [my_item for my_item in my_list]

prints out this new list as a whole

refer : here

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Ishan Khare
  • 1,745
  • 4
  • 28
  • 59
  • 3
    *"no-matter we use print() or print in 2.7 or 3.x"* This is only true for python 2.x, not python 3.x. `[print(char) for char in "abc"]` is valid (but discouraged) in python 3.x – jDo Apr 28 '16 at 19:22
7

It's a syntax error because print is not a function. It's a statement. Since you obviously don't care about the return value from print (since it has none), just write the normal loop:

for my_item in my_list:
    print my_item
Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
0

print in python 3 makes it more obvious on how to use it.

the square brackets in the list comprehension denotes that the output will actually be a list. L1=['a','ab','abc'] print([item for item in L1]) This should do the trick.