python – Why does map not print the list


Here is my code:

l = [1,2,3,4]
map(lambda x: print(x), l)

Why doesn't it display the list?


map is a lazy object, print is applied to the next l element only when the next element is requested from the map itself. You can force calculations, for example, by wrapping map in a list

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

or in some other way force map to return all elements

'что-то, чего точно нет в результирующей коллекции' in map(lambda x: print(x), l)
for _ in map(lambda x: print(x), l):

But it's still better to use map only when the result is important. In your case, only the side effect is important – the output to the screen. A regular for in this situation would be much more appropriate.

Scroll to Top