Sum of number digits in List in Python Last Updated : 03 May, 2025 Comments Improve Suggest changes Like Article Like Report Our goal is to calculate the sum of digits for each number in a list in Python. This can be done by iterating through each number, converting it to a string, and summing its digits individually. We can achieve this using Python’s built-in functions like sum(), map(), and list comprehensions. For example, given the list [123, 456, 789], we will calculate the sum of digits for each number, resulting in a list of sums such as [6, 15, 24].Using LoopsThis code calculates the sum of digits for each number in the list a using a while loop and stores the results in the res list. Python a = [123, 456, 789] res = [] for val in a: total = 0 while val > 0: total += val % 10 val //= 10 res.append(total) print(res) Output[6, 15, 24] Explanation: The code loops through each number in the list a, extracting and summing its digits.It uses the modulo operator (%) to get the last digit and integer division (//) to remove the last digit, continuing this until the number is reduced to zero.The sum of digits for each number is stored in the list res.Using List ComprehensionList comprehension offers a more concise way to sum digits. This code calculates the sum of digits for each number in the list a using a list comprehension and stores the results in the res list. Python a = [123, 456, 789] res = [sum(int(digit) for digit in str(val)) for val in a] print(res) Output[6, 15, 24] Explanation:str(val) converts the number to a string so we can loop through its digits.int(digit) converts each string digit back to an integer for summing.The list comprehension sums the digits of each number in the list.Using map Functionmap() function is a functional approach that applies a function to each element in the list. Python a = [123, 456, 789] res = list(map(lambda val: sum(int(digit) for digit in str(val)), a)) print(res) Output[6, 15, 24] Explanation: The lambda function directly computes the sum of digits and map() function is used to map this lambda function for each value in the list "a".Related Articles:Python While LoopList Comprehension in PythonPython map() function Comment More infoAdvertise with us Next Article Sum of number digits in List in Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Sum the Digits of a Given Number - Python The task of summing the digits of a given number in Python involves extracting each digit and computing their total . For example, given the number 12345, the sum of its digits is 1 + 2 + 3 + 4 + 5 = 15. Using modulo (%)This method efficiently extracts each digit using the modulus (%) and integer di 2 min read Python - Get summation of numbers in string list Sometimes, while working with data, we can have a problem in which we receive series of lists with data in string format, which we wish to accumulate as list. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + int() This is the brute force method to perform this 3 min read Python - Sort list of numbers by sum of their digits Sorting a list of numbers by the sum of their digits involves ordering the numbers based on the sum of each individual digit within the number. This approach helps prioritize numbers with smaller or larger digit sums, depending on the use case.Using sorted() with a Lambda Functionsorted() function w 2 min read Find Sum and Average of List in Python Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list.Using 2 min read Python - Convert Number to List of Integers We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let's discuss several methods to achieve this conversion.Using map() and str() Combination of str() and map() is a straightforwa 3 min read Like