With map in Python, we apply a method to a collections. Map requires fewer statements and variables. It is declarative—we tell the program the result we want, not how to compute it.
When using map, it is tempting to pass a lambda as the first argument. But this is not always needed—sometimes we can pass a function name like len()
directly.
Here we apply map()
to a small list. As the first argument to map, we pass a lambda expression. This lambda increments each element it receives.
iterable
collection containing the elements of the list. All elements have had 1 added to their values.# An input list. items = [1, 2, 3] # Apply lambda to all elements with map. for r in map(lambda x: x + 1, items): print(r)2 3 4
With map, the lambda is not required. A def-function may be passed to the map built-in method. Or a method like len
can be used.
elements = ["", "ab", "abcd"] # Call len on all elements and print all lengths. results = map(len, elements) for result in results: print(result)0 2 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.
# Original list. items = [7, 8, 9] # Map into a new list. items2 = list(map(lambda z: z * 2, items)) # Display two lists. print(items) print(items2)[7, 8, 9] [14, 16, 18]
How many times in a collection is a condition true? A predicate method is one that returns true or false based on its argument.
sum()
to count true results of the predicate.string
parameter. It calls the startswith
method.# Cities. names = ["San Jose", "San Francisco", "Santa Fe", "Houston"] # Sum result of map. count = sum(map(lambda s: s.startswith("San"), names)) # Count of cities starting with San. print(count)3
More than one iterable
can be used as arguments to map. Here we use two lists in a map call. The two lists have an unequal number of elements.
# Two input lists. a = [1, 2, 3] b = [2, 3, 4, 5] # Multiply elements of two lists together. result = list(map(lambda x, y: x * y, a, b)) # Three elements are present. print(result)[2, 6, 12]
In tests, map tends to be slower than an equivalent for
-loop. There is some overhead to invoking a method and constructing a map result.
map()
was slower. Map()
may be best reserved for situations where the clarity of code (not its speed) is more important.import time numbers = [5, 10, 15, 20, 30, 40] print(time.time()) # Version 1: map. for c in range(0, 1000000): sum = 0 for i in map(lambda v: v + 20, numbers): sum += i print(time.time()) # Version 2: for-loop. for c in range(0, 1000000): sum = 0 for v in numbers: sum += (v + 20) print(time.time())1411254268.364547 1411254271.106704 map: 2.742 s 1411254272.373777 for-loop: 1.267 s
Solutions that use map can become complex. Sometimes, using statements in a loop is simpler. In programming, "advanced" code is often inferior to understandable code.
Simpler things, like for
-loops, are often easier to keep correct. Map meanwhile has benefits in programs. Entire programs can be designed around it.