By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Why is map() preferable in this case?

P: n/a
Ray
Hello,

I'm just reading the latest edition of Python Cookbook, where it says
in Recipe 4.2:

"when the op you wanna perform on each item is to call a function on
the item and use the function's result, use L1 = map(f, L), rather than
L1 = (f(x) for x in L)"

What is wrong with the generator expression (or maybe it is list
comprehension, I cannot remember now whether they used [] or () in the
book)? Is it for clarity? I'm a newbie, and to me, the
generator/comprehension looks _way_ more clearer than map(f, L).

Are there any performance/memory requirements I'm not aware of? Why
would one want to use map() when there's already an expression that is
so clear and easy to understand?

Thanks!
Ray

Sep 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ray wrote:
Hello,

I'm just reading the latest edition of Python Cookbook, where it says
in Recipe 4.2:

"when the op you wanna perform on each item is to call a function on
the item and use the function's result, use L1 = map(f, L), rather than
L1 = (f(x) for x in L)"

What is wrong with the generator expression (or maybe it is list
comprehension, I cannot remember now whether they used [] or () in the
book)? Is it for clarity? I'm a newbie, and to me, the
generator/comprehension looks _way_ more clearer than map(f, L).

Are there any performance/memory requirements I'm not aware of? Why
would one want to use map() when there's already an expression that is
so clear and easy to understand?

Thanks!
Map is in C. It's faster, but not as clear. Some people do think map(f,
L) is nicer though. Google is your friend here, if you want to read the
old arguments. Ray


Sep 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.