You can use the map
built-in function here:
result_iter = map(max, list_one, list_two)
which will create a list on Python 2 and an iterator (a map object) on Python 3 - if you do need a list, on Python 3 wrap the map
with list()
:
result_list = list(map(max, list_one, list_two))
Example:
>>> list_one = [1, 2, 6]
>>> list_two = [3, 2, 4]
>>> list(map(max, list_one, list_two))
[3, 2, 6]
How this works is that the map
function takes 1 function as an argument, followed by 1 to n iterables; the iterables are iterated over simultaneously and their values are passed as arguments to the given function; whatever is returned from the function (max
in this case) is yielded from the map
into the result value.