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
# 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)) |
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
# 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)) |
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
# 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:
Python3
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
# 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. |
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
- Initialize a variable digit_sum to 0.
- Apply a while loop to extract the last digit of the number using the modulo operator (%) and add it to the digit_sum variable.
- Divide the number by 10 using floor division operators (//) to remove the last digit.
- Repeat sub point 2 and 3 until the number becomes zero.
- Return the digit_sum variable.
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:
- Apply map() function to the digit_sum() function.
- Convert the resulting map object to a list using the list() function.
- Return the resulting list.
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 :
Python3
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)) |
[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
- For each elem in the list, the inner list creates a new list that consists of the sum of the individual digits.
- The str() function converts the integer to a string.
- The int() function converts each digit back to an integer.
- The sum() function finds the sum.
- The outer list adds the sum calculated in step 4 to a new list.
Python3
# 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))) |
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.