
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python | Sum of number digits in List
When it is required to sum the number of digits in a list, a simple loop and the ‘str’ method can be used.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
The ‘str’ method converts the given value into a string data type.
Below is a demonstration for the same −
Example
my_list = [11, 23, 41, 62, 89, 0, 10] print("The list is : ") print(my_list) my_result = [] for elem in my_list: sum_val = 0 for digit in str(elem): sum_val += int(digit) my_result.append(sum_val) print ("The result after adding the digits is : " ) print(my_result)
Output
The list is : [11, 23, 41, 62, 89, 0, 10] The result after adding the digits is : [2, 5, 5, 8, 17, 0, 1]
Explanation
- A list is defined, and is displayed on the console.
- Another empty list is created.
- The list is iterated over, and every element in the list is converted to a string, and iterated over.
- It is then added and converted as a single digit.
- This is done on all elements of the list.
- This is appended to the empty list.
- It is then displayed as output on the console.
- Related Articles
- Find smallest number with given number of digits and sum of digits in C++
- Program to find the sum of all digits of given number in Python
- Find the Largest number with given number of digits and sum of digits in C++
- Prime digits sum of a number in JavaScript
- Python Program to Find the Sum of Digits in a Number without Recursion
- Program to find minimum digits sum of deleted digits in Python
- Program to count number of elements in a list that contains odd number of digits in Python
- Digit sum upto a number of digits of a number in JavaScript
- Program to find sum of digits until it is one digit number in Python
- Product sum difference of digits of a number in JavaScript
- Convert list of tuples into digits in Python
- Find sum of digits in factorial of a number in C++
- Destructively Sum all the digits of a number in JavaScript
- Finding sum of digits of a number until sum becomes single digit in C++
- Python program to find the sum of all even and odd digits of an integer list

Advertisements