72
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.

1
  • 4
    That's a syntax error in Python 2, so I'd assume Python 3. Commented Oct 11, 2011 at 19:15

4 Answers 4

111

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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)).
print(x) isn't a syntax error in Python < 3. Edit: Sorry, it's syntax error in this question.
@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 ;)
list(map(lambda x:print(x) if x>=a and isIntPalindrome(x),primes)) Ok, but why that doesn't work (syntax)
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 Nones. 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.
|
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?

2 Comments

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".
you can actually do map(print, primes) directly, without defining a lambda function.
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.

1 Comment

Thanks, I am long gone from python, but that's an elegant way imho.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.