I have a question about how I can add each item of 2 lists and play the results of the sum to a third in python

Asked

Viewed 38 times

0

I have the following program

#Faça um programa  que percorra duas listas e gere uma terceira sem elementos repetidos.

x = []

y = []

z = []

while True:

    n = int(input("digite um número(0 sai): "))

    if n == 0:

        break

    x.append(n) and y.append(n + 1)

c = x[x] + n[x] 

#aqui sai a falha com TypeError: list indices must be integers or slices, not list


while c < len(z):

    z = [c]

    print(z[x])

    c += 1

my doubt is how do I add each item of the lists x and y, after that add these items in the list z, how can I do this?

1 answer

-1

You want to add the elements of the two lists and put the result in a third or you want to join the elements of the two lists in a third without repeating?

Case 1 (sum):

x=[1,3,4]
y=[1,2,3]
z=[]
for i in range(len(x)):
      z.append(x[i]+y[i])

Upshot:

z=[2,5,7]

Case 2 (attach):

x=[1,3,4]
y=[1,2,3]
z=x+y
z=list(dict.fromkeys(z))
print(z)

Upshot:

z=[1,3,4,2]

That satisfies your doubt?

  • that’s right, the exercise as it is for me doesn’t necessarily specify whether I need to add or concatenate the 2 both ways how it doesn’t have something exact I believe it works, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.