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 item
The main advantage of map
is when you want to get the result of some calculation on every element in a list. For example, this snippet doubles every value in a list:
map(lambda x: x * 2, [1,2,3,4]) #=> [2, 4, 6, 8]
It is important to note that map
returns a new list with the results. It does not modify the original list in place.
To do the same thing with for
, you would have to create an empty list and add an extra line to the for
body to add the result of each calculation to the new list. The map
version is more concise and functional.