Sum of Number Digits in List Using Python

0

Sum of Number Digits in List Using Python

  • The problem of finding summation of digit of number is quite common. This can sometimes come in form of list and we need to perform that.

Source Code:

# Sum of number digits in List
# using loop + str()

# Initializing list
test_list = [12, 67, 98, 34]

# printing original list
print("The original list is : " + str(test_list))

# Sum of number digits in List
# using loop + str()
res = []
for ele in test_list:
    sum = 0
    for digit in str(ele):
        sum += int(digit)
    res.append(sum)
    
# printing result
print ("List Integer Summation : " + str(res)) 
 

Post a Comment

0Comments
Post a Comment (0)