Sumar dos listas elemento por elemento

rahim -  
 S -
Bonjour,

You're starting with 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 get
L1 [1, 2, 3]
+
L2 [4, 5, 6, 7]
-----------------------
L3 = [5, 7, 9, 7]

column by column from left to right even if my two lists are of different lengths.

How to do ?

Thank you

1 respuesta

  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)):]

    y debería funcionar.
    21