Python's zip, map, and lambda

A simple explanation

Let us assume that we have got a and b: two lists of integers. The goal is to merge them into one list, keeping whichever value is the largest at each index.

>> a = [1, 2, 3, 4, 5]
>> b = [2, 2, 9, 0, 9]

We can write a simple function that compares each item from a and b, then stores the largest in a new list as shown below:

def pick_the_largest(a, b):
    result = []  # A list of the largest values
    # Assume both lists are the same length
    list_length = len(a)
    for i in range(list_length):
        result.append(max(a[i], b[i]))
    return result

however the pythonic way to write is by a single line !

>> map(lambda pair: max(pair), zip(a, b))
[2, 2, 9, 4, 9]

Let us understand these functions one by one,


zip function takes two equal-length collections, and merges them together in pairs. If we use this on our a and b, we get,

>>> zip(a, b)
    (1, 2),
    (2, 2),
    (3, 9),
    (4, 0),
    (5, 9)


lambda is a shorthand to create an anonymous function. It’s often used to create a one-off function (usually for scenarios when you need to pass a function as a parameter into another function). It can take a parameter, and it returns the value of an expression.

Now, assuming that we have a a pair of values, we can create a function that picks the larger of the pair:

>>> lambda pair: max(pair)


map takes a function, and applies it to each item in an iterable (such as a list). Thus putting it all together we get the single line pythonic solution,

>>> map(  # apply the lambda to each item in the zipped list
        lambda pair: max(pair),  # pick the larger of the pair
        zip(a, b)  # create a list of tuples
[2, 2, 9, 4, 9]

This example originally appeared here