Matching a list equivalent to a map in two lists in parallel

I understand that with one argument, the list map()can be replaced by a list comprehension. for instance

map(lambda x : x**2, range(5))

can be replaced by

[x**2 for x in range(5)]

Now, how would I do something similar for two parallel lists. In other words, I have a line of code with the following pattern:

map(func, xs, ys)

where func()takes two arguments.

How can I do the same with the list?

+4
source share
1 answer

map() zip() function . zip() , :

[func(x, y) for x, y in zip(xs, ys)]

, map(func, a1, a2, .., an) [func(*args) for args in zip(a1, a2, .., an)].

+8

Source: https://habr.com/ru/post/1664872/


All Articles