Adding two lists element by element

rahim -  
 S -
Hello,

I am new to Python

My question is as follows:

I have two lists
L1 = [1, 2, 3] with 3 elements
L2 = [4, 5, 6, 7] with 4 elements
I want to add them element by element to obtain
L1 [1, 2, 3]
+
L2 [4, 5, 6, 7]
-----------------------
L3 = [5, 7, 9, 7]

column by column from left to right, even though my two lists have different lengths.

How can I do this?

Thank you

1 answer

  1. S
     

    [L1[i]+L2[i] for i in range(min(len(L1),len(L2)))]+max(L1,L2,key=len)[min(len(L1),len(L2)):]

    and it should work.
    21