From what I understand, you’re willing to go through both lists simultaneously and add up the corresponding values of the two list, index by index. For this you should realize that there are three different cases to solve, which are:
- Case 1: The two lists have the same size;
- Case 2: The first list has smaller size;
- Case 3: The first list has larger size;
In order to solve this issue in a very comprehensive way, that is, that it can encompass the three cases mentioned above, we can implement the following algorithm:
from itertools import zip_longest
m = []
x = [1, 2, 3]
y = [4, 5, 6, 7, 8, 9]
if len(x) < len(y):
for e, i in zip_longest(x, y, fillvalue=None):
if e is not None:
f = e
g = i
h = f + g
else:
h = i
m.append(h)
elif len(x) == len(y):
for e, i in zip(x, y):
h = e + i
m.append(h)
else:
for e, i in zip_longest(y, x, fillvalue=None):
if e is not None:
f = e
g = i
h = f + g
else:
h = i
m.append(h)
print(m)
Note that the block if checks if the list size x
is minor that the size of the list y
and, if so, these two lists shall be travelled using the zip_longest
by completing the missing items on the list x
with the value None
. Then sum each matching of index-to-index elements by storing them in the list m
and then displays the list m
.
Already the block Elif checks if the list size x
is equal to the size of the list y
and, if it is, these two lists will be covered with the help of the zip
, adding each match of index-to-index elements, storing them in the list m
and then displayed the list m
.
And last we have the block Else. This block will be executed if the list size x
be it greater that the size of the list y
. In this case the two lists are covered using the method zip_longest
by completing the missing items on the list y
with the value None
. Then sum each match of index-to-index elements, store them in the list m
and then displays the list m
.
Now, if you need to generalize this problem to any two lists, you can use the following algorithm:
from itertools import zip_longest
x = list(map(int, input('Digite os valores da lista "X": ').split()))
y = list(map(int, input('Digite os valores da lista "Y": ').split()))
m = []
if len(x) < len(y):
for e, i in zip_longest(x, y, fillvalue=None):
if e is not None:
f = e
g = i
h = f + g
else:
h = i
m.append(h)
elif len(x) == len(y):
for e, i in zip(x, y):
h = e + i
m.append(h)
else:
for e, i in zip_longest(y, x, fillvalue=None):
if e is not None:
f = e
g = i
h = f + g
else:
h = i
m.append(h)
print(m)
In this second algorithm, when the following sentence appears Digite os valores da lista "X":
, you must type all the elements of the list X
, on the same line, separated by a single space and then press enter
. And when the next sentence appears Digite os valores da lista "Y":
, you must type all the elements of the list Y
, on the same line, separated by a single space and then press enter
.
From that time the algorithm will perform the rest of the operations.
a tip for you to be able to solve this is to do the
for
to the largest and smallest list you make aif
checking whether she’s finished or not– Codigo de Senior