How can I get rid of having lists inside of a list in python? -


    def digit_sum(n):         n = str(n)         empty = [x.split() x in n]         print empty        digit_sum(21) 

this code output:

     [['2'], ['1']] 

what need make it:

    [2, 1]  

so can add numbers in list together. how do that?

i (you don't need .split it, convert string on can iterate):

def digit_sum(n):     empty = [int(d) d in str(n)]     print empty 

demo:

>>> digit_sum(21) [2, 1] 

you add them sum() function.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -