- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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)
The list is : [11, 23, 41, 62, 89, 0, 10] The result after adding the digits is : [2, 5, 5, 8, 17, 0, 1]
- 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.
- Find smallest number with given number of digits and sum of digits in C++
- Find the Largest 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
- Program to find minimum digits sum of deleted digits in Python
- Python Program to Find the Sum of Digits in a Number without Recursion
- Find sum of digits in factorial of a number in C++
- Finding sum of digits of a number until sum becomes single digit in C++
- Program to count number of elements in a list that contains odd number of digits in Python
- Convert list of tuples into digits in Python