In loops, we call methods, but sometimes this leads to code that is unclear.
It is hard to maintain, and bugs emerge. with map, we declareatively apply methods to collections.
Map requires fewer statements and variables. It is a form of declarative programming. We tell
the program the result we want, not how to compute it.
Example:
items=[1,2,3]
for r in map(lambda x: x+1, items):
print r
Output:
2
3
4
Map returns an iterator. we often must convert this back into the desired collection type.
here, we use map on a list. we then convert the result of map back into a list.
Example:
items=[7,8,9]
items2=list( map(lambda x: x*2, items)):
print items
print items2
Output:
[7,8,9]
[14, 16, 18]