18

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 function:

l = [1, 2, 3, 4, 5]
l = map(print, l)
print l

python will complain:

l = map(print, l)
            ^
SyntaxError: invalid syntax

What makes print special? Doesn't print(x) also a valid function call? The above code are tested under python 2.7.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
clwen
  • 20,004
  • 31
  • 77
  • 94
  • Very related: https://stackoverflow.com/questions/7731213/print-doesnt-print-when-its-in-map-python (this one concerns syntax error -- statement/function difference in Python 2, that one is about `map` being lazy) – user202729 Aug 04 '18 at 02:48
  • Possible duplicate of [Why doesn't print work in a lambda?](https://stackoverflow.com/questions/2970858/why-doesnt-print-work-in-a-lambda) (different use case, same issue about statement/function) – user202729 Aug 04 '18 at 02:53

6 Answers6

22

In Python 2.x, print is a statement, not a function. If you try this in Python 3.x it will work.

In Python 2.x, you can say print(x) and it is not a syntax error, but it isn't actually a function call. Just as 1 + (3) is the same as 1 + 3, print(x) is the same as print x in Python 2.x.

In Python 2.x you can do this:

def prn(x):
    print x

Then you can do:

map(prn, lst)

and it will work. Note that you probably don't want to do lst = map(prn, lst) because prn() returns None, so you will replace your list of values with a same-length list of just the value None.

EDIT: Two other solutions for Python 2.x.

If you want to completely change the behavior of print, you can do this:

from __future__ import print_function

map(print, lst)

This makes print into a function just as it is in Python 3.x, so it works with map().

Or, you can do this:

from pprint import pprint

map(pprint, lst)

pprint() is a function that prints things and it is available as a built-in. I'm not exactly sure how it is different from the default print (it says it is a "pretty-print" function but I'm not sure how exactly that makes it different).

Also, according to the PEP 8 standard, it is non-recommended to use l as a variable name, so I am using lst instead in my examples.

http://www.python.org/dev/peps/pep-0008/

steveha
  • 74,789
  • 21
  • 92
  • 117
  • It will not blow up, but very likely it will not work as expected. I guess clwen wanted to actually print stuff and not get ``. If you want to print it, you have to iterate over the map object. – Martin Thoma Nov 07 '13 at 18:44
  • I'm not sure what you mean by "it will not blow up"... if you try running `map(print, [])` in Python 2.x you will get `SyntaxError: invalid syntax` because `print` is a statement. You must have tried it in Python 3.x, where `print` is a function and it's perfectly legal to pass it to `map()`. In Python 2.x `map()` returns a list, not a ``. – steveha Nov 07 '13 at 20:27
  • I meant: Yes, you're correct, `map(print, [])` will not blow up (throw an exception) in Python 3. But although it does not throw an exception, the result is probably not what clwen wanted to get. – Martin Thoma Nov 07 '13 at 21:57
8

A better way to map print in 2.x would be to do

from __future__ import print_function
Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
  • 1
    If you do this, you can no longer use `print` as a statement in that module. As long as that is understood this is a reasonable thing to do. – steveha Aug 01 '12 at 22:52
4

As others have said, in Python 2.x print is a statement. If you really want to do this in Python 2.x you can use pprint:

from pprint import pprint
l = [1, 2, 3, 4, 5]
p = map(pprint, l)
mVChr
  • 49,587
  • 11
  • 107
  • 104
2

Because print is not a function.

But you can make print-wrapper, of course:

>>> def p(x):
...   print x
... 
>>> l = [1, 2, 3, 4, 5]
>>> l = map(p, l)
1
2
3
4
5
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
1

From your line print l, I assume this is python2, where print is not a function, it's a statement.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

The above answers work for Python 2, but not in Python 3 with the changes to both map and the print function.

The solution I've arrived at to achieve what I wanted map(print, lst) to do in Python 3 is to unpack the list inside of the print call.

lst = [1, 2, 3]
print(*lst, sep='\n')

Output:

1
2
3

You can find more detail around this in my answer to Use print inside lambda.

Community
  • 1
  • 1
Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76