Home
Map
map ExamplesUse the map built-in function to call a function on each element in a list. A lambda can be used.
Python
This page was last reviewed on Aug 27, 2021.
Map. 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.
Shows a map
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.
An example. 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.
String List
Then The map method passes all elements of the list to the lambda. It does this one at a time.
Result We receive an iterable collection containing the elements of the list. All elements have had 1 added to their values.
Shows a map
# 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
No lambda. 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
Iterator. 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.
Warning This code creates a copy of the original list. Both exist in memory. This may be inefficient if you want to modify 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]
Sum, predicate. How many times in a collection is a condition true? A predicate method is one that returns true or false based on its argument.
Here We use map with a predicate method. Then we use sum() to count true results of the predicate.
Detail We use a lambda expression: this one receives a string parameter. It calls the startswith method.
And They are counted by sum. Three of the four strings start with the substring "San" in the list.
# 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
Two iterables. 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.
However Map does not care. It proceeds as far as it can, which is three elements.
Tip The lambda here accepts two arguments. This is required when using two iterables.
Info The first argument is the first list's element. And the second argument is from the other list.
# 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]
Performance. 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.
Version 1 In this version of the code we invoke the map method, and loop over the results of map.
Version 2 We directly loop over a list, and use the same logic as version 1 but without the map in vocation.
Result Calling 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 27, 2021 (image).
Home
Changes
© 2007-2024 Sam Allen.