Python Map

map(function, iterable1, iterable2, ...) – apply a function to each element of a bunch of things (technically, an iterable e.g., list, tuple, sets), get back an equivalently long bunch of results.

things = [1, 2, 3]

def square(thing):
  return(thing*thing)
  
results = map(square, things)
# [1, 4, 9]

More concisely, the applied function can be a lambda function as well.

things = [1, 2, 3]
  
results = map(lambda x: x*x, things)
# [1, 4, 9]

Multiple things can be passed in:

things1 = [1,2,3]
things2 = [4,5,6]

results = map(lambda uno, dos: uno + dos, things1, things2)
# [5, 7, 9]

^ In this case, we’re saying: take these two things, and add the elements of the things together.

Resources #

For a more detailed treatment: