Print doesn't print when it's in map, Python

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.


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?


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.