How do you sum elements from lists that you have created with loops and are initially not visible?
If you say you create n` times lists and you will sum the elements in the first column, second column and so on.
x=int(input("")
lst[]
for i in range(x):
loop= input("")
lst.append(loop)
CodePudding user response:
could just use the built-in function sum, that will sum all the values from the list lst, could:
x=int(input("range >>") #user inputs the range
lst = [] #list is created
for i in range(x):
loop= int(input("Number >>")) #creates a number that will insert in the list 'lst'
lst.append(loop) #appending the value
print("sum:",sum(lst))
Output:
range >>3
Number >>4
Number >>4
Number >>4
sum:12
CodePudding user response:
A compact way would be:
print(sum(int(input('enter number: ')) for _ in range(int(input('enter x: ')))))
CodePudding user response:
you can use list generator to convert them to int, and then use sum function:
s = sum([int(i) for i in lst])
