What am i doing wrong in python

Hello everyone.
I was attempting the credit problem in problem set 1 from CS50,
and i was stuck at adding the digits of numbers after multiplying them by 2.
When i used sum(), i got sum of numbers so if there is a two digit number in my list it will add the number itself to other single digit numbers and not the individual digits of the numbers.
For example:

list1 = [0,4,1,2,3,2,8,9,6]
 x= int(len(list1))
## multiplying each number in list by 2.
for index in range(x):
    product1 = [i*2 for i in list1]
this returns 
>>>  [0,8,2,4,6,4,16,18,12]
now if i use sum(product1)
i get 0+8+2+4+6+4+16+18+12 = 70
however, what i want to do instead is 
0+8+2+4+6+4+1+6+1+8+1+2 = 43

how can i acheive this.
Please help.
Thanks in advance.

hi @sourabh I am not a python dev, however I would first check inside the loop how many digits the current product has, then break each number that has 2 or more digits into an array so that I can then sum their digits and only then print product1.

Hope that’s helpful :slightly_smiling_face::+1:

Thanks @palmaone after lots of googling i came up with this:

sum_product = 0
for elemet in product1:
     my_str =  str(element)
     for i in range(len(my_str)):
          sum_product += int(my_str[i])

this seems to do the job.
this is basically converting each element in the list product1
into an string and and adding each letter after converting it to an integer for each index value of that string in the list.

but do let me know if there is more efficient way of doing this.

1 Like


Here is a more concise way of solving the problem. Whether it is more readable is debatable.

list1 = [0,4,1,2,3,2,8,9,6]
sum_product = sum(int(digit) for digit in list(''.join([str(num *2) for num in list1])))

A less concise but more readable version would be:

list1 = [0,4,1,2,3,2,8,9,6]
product1 = [num *2 for num in list1]
sum_product = 0
for element in product1:
  digits = [int(digit) for digit in list(str(element))]
  sum_product += sum(digits)

Thanks @RandellDawson i really appreciate the feedback.
The first example is a great way to do it in one line of code, however, as I am a beginner in the world of coding, I think i’ll stick to the longer version for now as it is easier to understand for me.
As I haven’t quite used the join function till now i don’t understand how it operates but ill start as soon as possible on that one and some others.
Thanks for the help, I really appreciate it.

Ps: any tips to learn more about coding are always welcome.

You can also use this:
t1=[0, 4, 1, 2, 3, 2, 8, 9, 6]
product=[i*2 for i in t1]
for num in product:
if num>9:
Newlist=[ int(d) for d in str(num) ]


@Nschadrack yeah I thought about it but if the number becomes a three digit number then I’ll have do this for three digit numbers as well.
That is not the case in the problem at hand but I was hoping to generalise it so that it can be used for any number of digits in a number.
Thanks for responding anyways.:metal::+1:

compact and concise code.

1 Like