Python map() built-in function

From the Python 3 documentation Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples.

Basics

The map function, map(function, iterable) takes in one or more iterables, a ‘callback function’ (often a lambda), and returns a “Map Object”. The map object contains the result of the map function applying the callback to each element in the iterable arguments. Map iterates over the provided iterable objects simultaneously. As in, at every step, “i” in the map function, the element at index “i” of each iterable will be available to the map function at that time. You will often want to cast the resultant map object to a list, tuple, or another form of object that is more convenient to work with once you are finished mapping.

***An important note: As of the release of Python 3, if the iterables supplied to map are of different lengths, The map will stop and return when it has hit the number of steps corresponding to the length of the shortest iterable.

Input Parameters:

Function: takes the item (or items) at the index corresponding to the current step of the Map and gives the return result as an item to store in the Map Object. The type of element stored to the map object will be identical to the type returned from the function.

Iterable(s): tuple, list, range, dictionary, set, string.

A very simple example:

def double_map(func, iter):
  my_map = map(func, iter)
  return list(my_map)

def double(element):
  return element*2

nums = [1,2,3,4]

print(double_map(double, nums))

Will output the following: [2,4,6,8]

A less simple example:

This example employs callback function written as a lambda, as well as

def multi_map(func, *iters):
  my_map = map(func, *iters)
  return list(my_map)

list1 = [1,2,3]
list2 = [4,5,6]

print(multi_map((lambda item1, item2: item1*item2), list1, list2))

Will output the following: [4,10,18]

An example demonstrating map’s handling of different iterable lengths:

def max(a,b):
  if a > b: return a
  else: return b

list1= [1,1,1]

list2= [0,0,0,1,1,1,1,1,1,1]

result = list(map(max,list1,list2))

print(result)

Will output the following: [1,1,1]

Notice how the length of the resultant list is equal to list1’s length?


Python abs() built-in function Python aiter() built-in function Python all() built-in function Python any() built-in function Python ascii() built-in function Python bin() built-in function Python bool() built-in function Python breakpoint() built-in function Python bytearray() built-in function Python bytes() built-in function Python callable() built-in function Python chr() built-in function Python classmethod() built-in function Python compile() built-in function Python complex() built-in function Python delattr() built-in function Python dict() built-in function Python dir() built-in function Python divmod() built-in function Python enumerate() built-in function Python eval() built-in function Python exec() built-in function Python filter() built-in function Python float() built-in function Python format() built-in function Python frozenset() built-in function Python getattr() built-in function Python globals() built-in function Python hasattr() built-in function Python hash() built-in function Python help() built-in function Python hex() built-in function Python id() built-in function Python __import__() built-in function Python input() built-in function Python int() built-in function Python isinstance() built-in function Python issubclass() built-in function Python iter() built-in function Python len() built-in function Python list() built-in function Python locals() built-in function Python map() built-in function Python max() built-in function Python memoryview() built-in function Python min() built-in function Python next() built-in function Python object() built-in function Python oct() built-in function Python open() built-in function Python ord() built-in function Python pow() built-in function Python print() built-in function Python property() built-in function Python range() built-in function Python repr() built-in function Python reversed() built-in function Python round() built-in function Python set() built-in function Python setattr() built-in function Python slice() built-in function Python sorted() built-in function Python staticmethod() built-in function Python str() built-in function Python sum() built-in function Python super() built-in function Python tuple() built-in function Python type() built-in function Python vars() built-in function Python zip() built-in function