Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a value in using map() vs for?

Does map() iterate through the list like "for" would? Is there a value in using map vs for?

If so, right now my code looks like this:

for item in items:
    item.my_func()

If it makes sense, I would like to make it map(). Is that possible? What is an example like?

like image 392
roder Avatar asked May 17 '09 19:05

roder


People also ask

What is difference between map and for?

map does exactly the same thing as what the for loop does, except that map creates a new array with the result of calling a provided function on every element in the calling array.

Is map () faster than for loop?

Comparing performance , map() wins! map() works way faster than for loop. Considering the same code above when run in this ide.

Can we use map instead of for loop in JavaScript?

Use map() instead of for() loopsmap() function works. If you only have knowledge of for() loops in JavaScript, this article will require you to understand the Arrow Function Expression syntax (a.k.a. “fat arrow” functions).

Which is faster for loop or map in JS?

As we can see in the picture, the built-in map function is much faster than the for-in loop. To be precise, it is 1.63x faster. There is no doubt that we should use a built-in map function.


2 Answers

You could use map instead of the for loop you've shown, but since you do not appear to use the result of item.my_func(), this is not recommended. map should be used if you want to apply a function without side-effects to all elements of a list. In all other situations, use an explicit for-loop.

Also, as of Python 3.0 map returns a generator, so in that case map will not behave the same (unless you explicitly evaluate all elements returned by the generator, e.g. by calling list on it).


Edit: kibibu asks in the comments for a clarification on why map's first argument should not be a function with side effects. I'll give answering that question a shot:

map is meant to be passed a function f in the mathematical sense. Under such circumstances it does not matter in which order f is applied to the elements of the second argument (as long as they are returned in their original order, of course). More importantly, under those circumstances map(g, map(f, l)) is semantically equivalent to map(lambda x: g(f(x)), l), regardless of the order in which f and g are applied to their respective inputs.

E.g., it doesn't matter whether map returns and iterator or a full list at once. However, if f and/or g cause side effects, then this equivalence is only guaranteed if the semantics of map(g, map(f, l)) are such that at any stage g is applied to the first n elements returned by map(f, l) before map(f, l) applies f to the (n + 1)​st element of l. (Meaning that map must perform the laziest possible iteration---which it does in Python 3, but not in Python 2!)

Going one step further: even if we assume the Python 3 implementation of map, the semantic equivalence may easily break down if the output of map(f, l) is e.g. passed through itertools.tee before being supplied to the outer map call.

The above discussion may seem of a theoretic nature, but as programs become more complex, they become more difficult to reason about and therefore harder to debug. Ensuring that some things are invariant alleviates that problem somewhat, and may in fact prevent a whole class of bugs.

Lastly, map reminds many people of its truly functional counterpart in various (purely) functional languages. Passing it a "function" with side effects will confuse those people. Therefore, seeing as the alternative (i.e., using an explicit loop) is not harder to implement than a call to map, it is highly recommended that one restricts use of map to those cases in which the function to be applied does not cause side effects.

like image 106
Stephan202 Avatar answered Oct 06 '22 23:10

Stephan202


You can write this using map like this:

map(cls.my_func, items)

replacing cls with the class of the items you are iterating over.

As mentioned by Stephan202, this is not recommended in this case.

As a rule, if you want to create a new list by applying some function to each item in the list, use map. This has the implied meaning that the function has no side effect, and thus you could (potentially) run the map in parallel.

If you don't want to create a new list, or if the function has side effects, use a for loop. This is the case in your example.

like image 26
Kiv Avatar answered Oct 07 '22 00:10

Kiv