70
primes = [2,3,5,7..] (prime numbers)
map(lambda x:print(x),primes)

It does not print anything. Why is that? I've tried

sys.stdout.write(x)

too, but doesn't work either.

Prateek Gupta
  • 119
  • 2
  • 7
matiit
  • 7,969
  • 5
  • 41
  • 65

4 Answers4

108

Since lambda x: print(x) is a syntax error in Python < 3, I'm assuming Python 3. That means map returns a generator, meaning to get map to actually call the function on every element of a list, you need to iterate through the resultant generator.

Fortunately, this can be done easily:

list(map(lambda x:print(x),primes))

Oh, and you can get rid of the lambda too, if you like:

list(map(print,primes))

But, at that point you are better off with letting print handle it:

print(*primes, sep='\n')

NOTE: I said earlier that '\n'.join would be a good idea. That is only true for a list of str's.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • 7
    It's still a very bad idea (non-idiomatic, not obvious - map, filter, list comprehensions, etc. are for computations, not for side effects) though. You should just use a for loop, or `print(*primes, sep='\n')`, or `print('\n'.join(str(x) for x in primes))`. –  Oct 11 '11 at 19:14
  • `print(x)` isn't a syntax error in Python < 3. Edit: Sorry, it's syntax error in this question. – utdemir Oct 11 '11 at 19:15
  • 1
    @utdemir: Occuring in a `lambda` or any other expression, it is (with or without parens). I assume OP meant that and you just want to nitpick on the wording ;) –  Oct 11 '11 at 19:17
  • list(map(lambda x:print(x) if x>=a and isIntPalindrome(x),primes)) Ok, but why that doesn't work (syntax) – matiit Oct 11 '11 at 19:17
  • @matiit: You can't use `if` like that. Try a list comprehension: `[print(x) for x in primes if x >= a and isIntPalindrome(x)]`, however using `print` in a list comprehension like that is still weird. Just use `[x for x in primes if ...]` and then `print` the result of that if you want to see the output. – Greg Hewgill Oct 11 '11 at 19:18
  • Sorry, I'm always using print on my codes, and I just tried doing print("a") in Python 2.7 and 2.5, so just thought it is valid. Thanks for correction. – utdemir Oct 11 '11 at 19:18
  • 1
    If you don't want to iterate over all of `primes` at once, or for some other reason you want separate `print` statements, the Pythonic way is a normal `for` loop, not `list` on a `map` which accumulates the `None`s. If you need to use `map` for some reason, you can avoid `None` accumulation with `next((x for x in map(print, primes) if x != None), None)`, but there is no reason to do that. – agf Oct 11 '11 at 19:42
6

This works for me:

>>> from __future__ import print_function
>>> map(lambda x: print(x), primes)
2
3
5
7
17: [None, None, None, None]

Are you using Python 2.x where print is a statement, not a function?

John Gaines Jr.
  • 11,174
  • 1
  • 25
  • 25
  • 3
    It works specifically because you're using Python 2. In Python 3, it doesn't. And OP is more likely using 3.x since `print` is used as function and since `sys.stdout.write` "doesn't work either". –  Oct 11 '11 at 19:18
  • 1
    you can actually do `map(print, primes)` directly, without defining a lambda function. – hitzg Sep 28 '15 at 17:22
4

Alternatively, you can unpack it by putting * before map(...) like the following

[*map(...)]

or

{*map(...)}

Choose the output you desire, a list or a dictionary.

3

Another reason why you could be seeing this is that you're not evaluating the results of the map function. It returns a generator (an iterable) that evaluates your function lazily and not an actual list.

primes = [2,3,5,7]
map(print, primes) # no output, because it returns a generator
primes = [2,3,5,7]
for i in map(print, primes):
    pass # prints 2,3,5,7

Alternately, you can do list(map(print, primes)) which will also force the generator to be evaluated and call the print function on each member of your list.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191