Python program to find the sum of number digits in list

Here, we will take a list as input from the user and return a list with the sum of number digits in the list.
Submitted by Shivang Yadav, on April 12, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

Sum of number digits is the sum of individual digits of the given number.

Example:

Number = 3254
Sum of digits = 3 + 2 + 5 + 4 = 14

Sum of number digits in the list in python

Problem statement

In this problem, we will take a list as input from the user. And print the sum of the number digits in the list.

Input:
[31, 65, 23, 90, 78]

Output:
[4, 11, 5, 9, 15]

We need to find the sum digits of a number using this method in Python.

Find the sum of number digits in list

For lists, python provides multiple methods to find the sum of digits for each element of the list. Python provides a few methods to perform the task.

Method 1: Using loop and finding digit Sum for each<

To find the digit sum, we will use a recursive call to a function that returns the sum of digits of a number for the given number.

We will loop for all elements of the array and for each element will we call the function to find the digit sum.

Algorithm

  1. Loop over the array and perform steps 2-4 for each value.
  2. Take the number, if it is 0, return zero
  3. If the number is not zero, go to step 2, with
    number = number / 10
  4. Return the value extracted from step 3 and add the last digit of the initial number.
  5. Print all values.

Program to find the sum of number digits in the list

# Python program to find sum of number digits

# Method to find the sum of number digits 
def sumDigits(num):
  if num == 0:
    return 0
  else:
    return num % 10 + sumDigits(int(num / 10))
    
# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

digitSumList = []
for ele in myList:
    digitSum = sumDigits(ele)
    digitSumList.append(digitSum)
    
# Printing values 
print("Entered List : ", myList)
print("Digit Sum List : ", digitSumList)

Output:

Enter number of elements : 5
12345
54321
123
567
89012
Entered List :  [12345, 54321, 123, 567, 89012]
Digit Sum List :  [15, 15, 6, 18, 20]

Method 2: Using str() method

An alternate method could be loop on the list and convert each element to a string and then perform addition for each digit of the element which will give the digitSum.

Algorithm

  • Loop over the list
    • Convert each element to a string and loop over the string.
      • Add all the digits of the list.
      • store the sum value of sumList.
  • Print the sumList.

Program to find the sum of number digits in the list using str() method

# Python program to find sum of number digits

# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

digitSumList = []
for ele in myList:
    digitSum = 0
    for singleDigit in str(ele):
        digitSum += int(singleDigit)
    digitSumList.append(digitSum)
    
# Printing values 
print("Entered List : ", myList)
print("Digit Sum List : ", digitSumList)

Output:

Enter number of elements : 5
12345
54321
123
456
890123
Entered List :  [12345, 54321, 123, 456, 890123]
Digit Sum List :  [15, 15, 6, 15, 23]

Method 3: Using list comprehension

Python's built-in comprehension feature that lets us reduce the number of lines in list creation to a single line.

We can use the list comprehension in place of loops. And for performing the sum we will use the built-in sum() function.

Program to find the sum of number digits in the list

# Python program to find sum of number digits

# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# finding the sum of number digits
digitSumList = list(map(lambda ele: sum(int(sub) for sub in str(ele)), myList))
    
# Printing values 
print("Entered List : ", myList)
print("Digit Sum List : ", digitSumList)

Output:

Enter number of elements : 5
12345
54321
123
456
890156
Entered List :  [12345, 54321, 123, 456, 890156]
Digit Sum List :  [15, 15, 6, 15, 29]

The list comprehension performs the same task as we have done in another method, which is for each element of the list we have converted to a string and found the sum of digits using the sum() method.

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.