Add the elements of two lists with lists

0

How can I add the elements of two lists. I have tried this but without success since it requires that the two lists have the same number of elements:

lista1 = [1, 2, 3, 4, 5, 6]
lista2 = [5, 6, 7, 8]

lista3 = []

for i, w in enumerate(lista1):
    lista3.append(lista1[i] + lista2[i])

print(lista3)

Thanks and best regards

    
asked by pelaitas 18.09.2017 в 03:11
source

2 answers

1

If you are looking for a list of the size of the largest but with the elements that share the index added, you can make a slight modification to your code:

mayor, menor = (lista1, lista2) if len(lista1) > len(lista2) else (lista2, lista1)
lista3 = []
for i, _ in enumerate(mayor):
    if i + 1 > len(menor):
      lista3.append(mayor[i])
    else:
      lista3.append(mayor[i] + menor[i])

print(lista3)

Basically:

  • We first establish which is the major list to iterate through the same
  • In the cycle if the index exceeds the length of the minor list, we simply copy the element of the largest, but add the element corresponding to the index of the two lists

Note: In for i, _ in enumerate(mayor): we use _ to ignore one of the elements received from enumerate , just the one we will not use, it is a good practice to do so.

Additionally, a simpler way to solve this problem is:

from itertools import zip_longest

result = [sum(n) for n in zip_longest(lista1, lista2, fillvalue=0)]
print(result)

Using zip_longest , which is nothing more than zip normal but that takes into account which is the largest list, what we achieve is to combine both lists creating "couples" and adding a 0 for the elements of the smallest list ( fillvalue=0 ), we would have something like this:

[(1, 5), (2, 6), (3, 7), (4, 8), (5, 0), (6, 0)]

Thus, we can apply a sum on each tuple without problems obtaining the expected result.

    
answered by 18.09.2017 в 04:19
0

To add the list1 that has more elements than the list2, you can enter the elements from list1 to list3 and then add to each item those from list2

    lista1 = [1,3,5,7,9,11]
    lista2 = [2,4,6,8,10]
    lista3 = []

    for i in range(len(lista1)):
      lista3.append(lista1[i])

    for i in range(len(lista2)):
      lista3[i] = lista3[i] + lista2[i]

    print(lista3)

This would give you as a result: [3,7,11,15,19,11]

To use this in other lists with different amount of elements you can evaluate the length of both and then do the procedure that I proposed to you

    lista1 = [1,3,5,7,9,11]
    lista2 = [2,4,6,8,10]
    lista3 = []

    x = len(lista1)
    y = len(lista2)

    if (x > y):
      for i in range(x):
         lista3.append(lista1[i])
      for i in range(y):
         lista3[i] = lista3[i] + lista2[i]
    elif (x < y):
      for i in range(y):
         lista3.append(lista2[i])
      for i in range(x):
         lista3[i] = lista3[i] + lista1[i]
    elif (x == y):
      for i in range(x):
         lista3.append(lista1[i]+lista2[i])

    print(lista3)
    
answered by 20.09.2017 в 01:39