Why this map do not work at command line?
Just fails silently:
python -c "x=range(1000);map(lambda l: print(l), x)"
The map should traverse the range as it works inside Python REPL.
You are probably trying to run with python2.x (to make sure try running python --version)
In python 2 lambda l: print(l) is not valid, since print is not a regular function/method.
Try running
python3 -c "x=range(1000);map (lambda l:print(l), x)΅
If you want a solution that is compatible with both versions, try:
python -c "x=range(1000); print('\n'.join(map(lambda l: str(l), x)))"
Bonus: A solution using list comprehensions:
python -c "x=range(1000); print('\n'.join([ str(l) for l in x ]))"
map(print, x) and map(str, x) are a little more terse, no need for lambdas.When you run this code (in python 3), the code is functioning correctly. However, that the code returns is a map object, which contains the instructions to print the numbers, as is shown here
>>> x=range(1000);map (lambda l:print(l), x)
<map object at 0x6ffffd0cba8>
To get the numbers to print, you would need to iterate over the map, like
>>> x=range(1000)
>>> y=map (lambda l:print(l), x)
>>> for n in y:
... pass
...
0
1
2
3
4
5
6
7
8
etc.
python3 -c ...-pythonis normally Python 2mapfor its side-effects is considered bad form; it's doubly bad because you rely onlambdafunctions unnecessarily (map(print, x)would be identical and not involve pointless slowdown/verbosity fromlambdawrapping). A better solution is either:print(*x, sep="\n")which prints all at once (fine for smallish output sequences, but not for millions of outputs) or just using an explicit loopfor l in x: print(l)(though the latter may not work well for one-liners). If you mustmap, justlist-ify to forcemapto run:list(map(print, x)).