Erich Erich - 3 years ago 83
Python Question

Element wise, variable combination of lists in python

Preface: Element-wise addition of 2 lists?

I want to write to code to have the following behavior:

[ 1, 1, ["Alpha"]]
+
[ 2, 2, ["Beta"] ]
|| || ||
\/ \/ \/
[3, 3, ["Alpha", "Beta"]]


in python. Is this possible without very messy comprehensions and mapping?

Answer Source
[a + b for a, b in zip(l1, l2)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download