 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- 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 −
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.
Advertisements
 