The map iterator lazily computes the values, so you will not see the output until you iterate through them. Here's an explicit way you could make the values print out:
def abc(x):
print(x)
it = map(abc, [1,2,3])
next(it)
next(it)
next(it)
The next
function calls it.__next__
to step to the next value. This is what is used under the hood when you use for i in it: # do something
or when you construct a list from an iterator, list(it)
, so doing either of these things would also print out of the values.
So, why laziness? It comes in handy when working with very large or infinite sequences. Imagine if instead of passing [1,2,3]
to map, you passed itertools.count()
to it. The laziness allows you to still iterate over the resulting map without trying to generate all (and there are infinitely many) values up front.