Sum two fields of list in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Sum two fields of list in python

How to Sum two fields of the list in python and sort it?

24th Apr 2020, 11:34 AM
Abbas Al-jubouri
Abbas Al-jubouri - avatar
4 Answers
+ 4
As the elements of the list are numbers in string dataformat, you can do this: - create a variable like sum_ - use a for loop that can iterate over the list. like: for i in lst: - you will get in each iteration an element from the list in loop varibale "i" - covert this value in i with int(i) to integer and add it to a variable that holds the sum of all converted numbers - after iteration is finished you have the sum of all values in the variable sum_ that can be printed
24th Apr 2020, 3:22 PM
Lothar
Lothar - avatar
+ 4
if you mean something like this: lst = [7, 4] print(sum(lst)) # output is 11 print(sorted(lst)) # output is [4, 7] If this is not what you have expected, you should rework your question and provide us with data samples for input and output.
24th Apr 2020, 11:42 AM
Lothar
Lothar - avatar
+ 2
I have list=[โ€œ12โ€,โ€33โ€,โ€5โ€] i want sum it
24th Apr 2020, 2:27 PM
Abbas Al-jubouri
Abbas Al-jubouri - avatar
+ 1
Please show us your try. In the meantime, here are some hints: ๐Ÿ“sum() is a built-in function that finds the sum of a list. It takes one parameter, the list. ๐Ÿ“sorted() is another built-in function, and it sorts a list. Once again, the only parameter is the list. Try joining the lists together, and make use of these functions.
24th Apr 2020, 11:43 AM
Jianmin Chen
Jianmin Chen - avatar