map() question...

Peter Hansen peter at engcorp.com
Mon Jan 13 04:04:10 CET 2003


Kevin Ethridge wrote:
> 
> Why doesn't map() work with the print function?
> ie map(print, ['Have', 'a', 'great', 'day.'])

I should have made another point in my reply, but since no one else
has made it yet instead, I will now.

It's bad form to use a side-effect in the way you are trying to use
it.  Map is intended to *return* a list, built by executing a 
function (which returns an item) on each item of the input list.
Using map just for the iteration effect is likely to confuse a
future programmer (which might be you!) so you should avoid it.

The better approach would be a simple for statement, which is the
one obvious solution to the problem and therefore the best:

for item in ['Have', 'a', 'maintainable', 'day.']:
    print item

-Peter




More information about the Python-list mailing list