Open In App

Python | Sum of number digits in List

The problem of finding the summation of digits of numbers is quite common. This can sometimes come in form of a list and we need to perform that. This has application in many domains such as school programming and web development. Let’s discuss certain ways in which this problem can be solved.
 

Method #1 : Using loop + str() 
This is brute force method to perform this particular task. In this, we run a loop for each element, convert each digit to string, and perform the count of the sum of each digit.
 






# Python3 code to demonstrate
# 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))

Output : 
The original list is : [12, 67, 98, 34]
List Integer Summation : [3, 13, 17, 7]

 

Time Complexity: O(n)
Auxiliary Space: O(n)



 
Method #2 : Using sum() + list comprehension 
This task can also be performed using shorthand using above functionalities. The sum() is used to compute summation and list comprehension is used to compute iterations.
 




# Python3 code to demonstrate
# Sum of number digits in List
# using sum() + list comprehension
 
# 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 sum() + list comprehension
res = list(map(lambda ele: sum(int(sub) for sub in str(ele)), test_list))
     
# printing result
print ("List Integer Summation : " + str(res))

Output : 
The original list is : [12, 67, 98, 34]
List Integer Summation : [3, 13, 17, 7]

 

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.

Method #3 : Using sum() + reduce()
This task can also be performed using shorthand using the above functionalities. The sum() is used to compute summation and reduce function from functools module.




# Python3 code to demonstrate
# Sum of number digits in a List
# using sum() + reduce()
from functools import reduce
 
# 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 sum() + reduce()
res = [reduce(lambda x, y: int(x) + int(y), list(str(i))) for i in test_list]
 
# printing result
print("List Integer Summation : " + str(res))

 Output:

The original list is : [12, 67, 98, 34]
List Integer Summation : [3, 13, 17, 7]

Method #4 : Using numpy

Note: Install numpy module using command “pip install numpy”

Here’s one approach using numpy:




import numpy as np
 
# 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 numpy
res = np.sum([list(map(int, str(ele))) for ele in test_list], axis=1)
 
# printing result
print("List Integer Summation : " + str(list(res)))

Output:

The original list is : [12, 67, 98, 34]
List Integer Summation : [3, 13, 17, 7]

Method #5 : Using itertools library:




# Python3 code to demonstrate
# Sum of number digits in List
# using itertools library
# importing itertools library
import itertools
# 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 itertools library
res = [sum(map(int, list(itertools.chain(*str(ele))))) for ele in test_list]
# printing result
print ("List Integer Summation : " + str(res))
#This code is contributed by Jyothi pinjala.

Output
The original list is : [12, 67, 98, 34]
List Integer Summation : [3, 13, 17, 7]

Time Complexity: O(n*m)
Auxiliary Space: O(n)

Method #6 : Using map() function and modulo operator :

Step-by-step algorithm for implementing the approach

1) First, define a function digit_sum(num) which takes a single number as input and returns the sum of its digits

2) Define a function sum_of_digits_list(lst) which will takes a list of numbers as input and returns a list containing the sum of digits of each number in the input list, using the following steps:

3) Define a list of numbers lst which will calculate the sum of digits.

4) Now wecall the sum_of_digits_list(lst) function with lst as the input.

5) Print the resulting list.

Code :




lst = [12, 67, 98, 34]
def digit_sum(num):
    digit_sum = 0
    while num > 0:
        digit_sum += num % 10
        num //= 10
    return digit_sum
 
def sum_of_digits_list(lst):
    return list(map(digit_sum, lst))
 
print(sum_of_digits_list(lst))

Output
[3, 13, 17, 7]

Time Complexity: O(nlog(n)) where n is the value of the input number. This is because the map() function applies the digit_sum() function to each element of the input list, which has a time complexity of O (log n) for each element. 
Auxiliary Space: O(n)  because it creates a list of length n to store the result of the map() function.

Method #7: Creating a expression without changing to a string




# 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
# creating an expression
res = list(sum(int(digit) for digit in str(num)) for num in test_list)
# printing result
print("List Integer Summation : " + str(list(res)))

Output
The original list is : [12, 67, 98, 34]
List Integer Summation : [3, 13, 17, 7]

Time Complexity: O(N*M) where N is the elements in the list and M is the average number of digits in each element.

Auxiliary Space: O(N) as we create a new list of the same size.


Article Tags :