Python Forum
Calculating the sum of a list's item.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating the sum of a list's item.
#1
Hello.
With this function
def digit_sum(number):
    value = str(number)
    digits = [int(x) for x in value]
    return sum(digits)
You can get the sum of one splited item given from the user. How can i make it so it does the same fuction but instead it will be given a list and lastly how can i replace the results that this function gave me with the number that it initially got in order to make the calculation? For example if it got the list

list=[1,25,5,30]
and it made the calculations,so we have as a unofficial result 7,3.My goal is to get the summed numbers which in case are 7 and 3 and add them to the list that the fuction got my numbers from. So we must have
list=[1,7,5,3]
Thank you

I think i got it

lista=[1,96,3]
lista_sum=[]
def digit_sum(LIST):
    for i in range(len(LIST)):
      value = str(LIST[i])
      digits = [int(x) for x in value]
      lista_sum.append(sum(digits))
      print (sum(digits))
      

digit_sum(lista)
print (lista_sum)
If you can suggest something please feel free to!
Reply


Messages In This Thread
Calculating the sum of a list's item. - by frequency - Nov-04-2018, 10:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Finding string in list item jesse68 8 1,932 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  how to easily create a list of already existing item CompleteNewb 15 3,669 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 5,892 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 3,545 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Time.sleep: stop appending item to the list if time is early quest 0 1,897 Apr-13-2021, 11:44 AM
Last Post: quest
  How to run a pytest test for each item in a list arielma 0 2,391 Jan-06-2021, 10:40 PM
Last Post: arielma
  How do I add a number to every item in a list? john316 2 2,009 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Ignoring a list item hank4eva 2 2,158 Aug-17-2020, 08:40 AM
Last Post: perfringo
  Select correct item from list for subprocess command pythonnewbie138 6 3,359 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  Why is the item not in list when it is DanielCook 2 2,056 Jul-08-2020, 07:38 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020