map

From CS 61A Wiki
Jump to: navigation, search

map is a built-in Python higher-order function that applies a given function to all the items of an iterable. The returned result differs between Python 2 and Python 3:

  • In Python 2, map returns a list, regardless of the type of iterable passed.
  • In Python 3, map returns an iterable map object, which can be converted into a sequence using the appropriate constructor (e.g., list(...), tuple(...)).

Forms

General form

The most general form of the map function is:

map(fn, itr)

where the function being applied is fn and the lone iterable is itr. fn must be a one-argument function.

Alternate forms

map has alternate forms:

  • map(fn, itr1, itr2, ...)
    When multiple iterables are offered, fn must take that many arguments. It will then apply the function to each of the iterables in parallel.
  • map(None, itr)
    In Python 2, when the given function is None, map assumes the identity function (lambda x: x). This only works for Python 2 — attempting this in Python 3 will result in a 'NoneType' object is not callable error.
If more than one iterable is provided, map will create tuples using elements from each iterable in the final list (e.g, the first element of the first iterable and the first element of the second iterable will be combined in a tuple to form the first element of the returned list).

Examples

Single-argument functions

increment

def increment(x):
    return x + 1
lst = [1, 2, 3, 4, 5]
 
# Python 2
>>> map(increment, lst)
[2, 3, 4, 5, 6] 
 
# Python 3
>>> list(map(increment, lst)
[2, 3, 4, 5, 6]

double

def double(x):
    return x * 2
lst = [1, 3, 5, 7, 9]
 
# Python 2
>>> map(double, lst)
[2, 6, 10, 14, 18]
 
# Python 3
>>> list(map(double, lst))
[2, 6, 10, 14, 18]

Multiple-argument functions

add_two_things
def add_two_things(x, y):
   return x + y
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]

Using the map function with the above arguments will combine items from matching indices of the two lists:

# Python 2
>>> map(add_two_things, lst1, lst2)
[5, 7, 9]
 
# Python 3
>>> list(map(add_two_things, lst1, lst2))
[5, 7, 9]
multiply_by_each_other

Suppose we wanted to multiply the elements of two lists by each other.

def multiply_by_each_other(x, y):
   return x * y
lst1 = [2, 4, 6]
lst2 = [5, 8, 10]

We can use the above function with map:

# Python 2
>>> map(multiply_by_each_other, lst1, lst2)
[10, 32, 60]
 
# Python 3
>>> list(map(multiply_by_each_other, list_a, list_b))
[10, 32, 60]

Using None as the function (only for Python 2)

Using None as the function is analogous to using the identity function.

A single iterable

In the case of a single list, map will simply return the identity of the list. We define lst and then pass it into map:

>>> lst = [7, 12, 33]
>>> map(None, lst)
[7, 12, 33]
Multiple iterables

Multiple iterables can passed as arguments along with None into the map function. We create three lists:

lst1 = [1, 2, 3, 4]
lst2 = [5, 7, 9, 11]
lst3 = [12, 14, 16, 18]

Using map:

>>> map(None, lst1, lst2, lst3)
[(1, 5, 12), (2, 7, 14), (3, 9, 16), (4, 11, 18)]

The first element from each list is combined into a tuple in the resulting list. The second tuple is a combination of the second elements, and so on.

Sources