OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

calculating the sum of digits in a list in python

  • Thread starter Thread starter Ahmed Anwer
  • Start date Start date
A

Ahmed Anwer

Guest
I am new to math problems in python but basically i have the following code:

Code:
a = list(range(1,10000))
str(a)

sum_of_digits = sum(int(digit) for digit in str(a[9998]))
print(sum_of_digits)

this allows me to calculate the sum of the digits of a given number in the list a. but instead of feeding numbers into this one by one, i want an efficient way to calculate the sum of the digits of all the numbers in a and print them all out at once. I can't seem to figure out a solution but i know the answer is probably simple. any help is appreciated!

edit: i didnt know this post would get this much attention, for those wanting more clarification i basically want to know which digits in the list of range 1,9999 has a sum of 34 or more. i think everyone thought i simply wanted to take the sum of digits of each list element and then compile a total sum. in any case, that method helped me solve the actual problem
<p>I am new to math problems in python but basically i have the following code:</p>
<pre><code>a = list(range(1,10000))
str(a)

sum_of_digits = sum(int(digit) for digit in str(a[9998]))
print(sum_of_digits)




</code></pre>
<p>this allows me to calculate the sum of the digits of a given number in the list a. but instead of feeding numbers into this one by one, i want an efficient way to calculate the sum of the digits of all the numbers in a and print them all out at once. I can't seem to figure out a solution but i know the answer is probably simple. any help is appreciated!</p>
<p>edit: i didnt know this post would get this much attention, for those wanting more clarification i basically want to know which digits in the list of range 1,9999 has a sum of 34 or more. i think everyone thought i simply wanted to take the sum of digits of each list element and then compile a total sum. in any case, that method helped me solve the actual problem</p>
 

Latest posts

Top