How to sum a list in python -


arr.rsplit(',', len(arr))     print sum(arr) 

if input string of "1,2,3,4", first line splits in list of 1,2,3,4. when print sum not work error message.

in case splitting string result not assigned again arr variable arr value not getting changed remains string, while apply sum(arr) giving error. if assign arr type of split elements <class 'str'> convert integer

i trying use split instead of rsplit solution in python 3 :

arr = "1,2,3,4" arr = map(int,arr.split(',')) print(sum(arr)) 

output : 10

it convert each element integer , take sum. if try print arr : print(arr) after map method gives output : <map object @ 0x7f90c081acc0> convert arr list access elements instead of arr = map(int,arr.split(',')) give arr = list(map(int,arr.split(',')))

if want use rsplit solution (in python 3):

arr = "1,2,3,4" arr = list(map(int,arr.rsplit(',', len(arr)))) print(sum(arr)) 

Comments