O'Reilly logo
  • Chris Kang thinks this is interesting:

The map built-in can do similar work, by generating the results of running items through a function, one at a time and on request. Like range, wrapping it in list forces it to return all its values in Python 3.X; this isn’t needed in 2.X where map makes a list of results all at once instead, and is not needed in other contexts that iterate automatically, unless multiple scans or list-like behavior is also required:

>>> list(map(sum, M))                        # Map sum over items in M


Cover of Learning Python, 5th Edition


map() runs the function, sum, over each items in M. In this case, each item in M is a list of numbers. So, sum() is called on all items in each such list in M to get a sum value and map calls this on each such list in M.