Python Forum

Full Version: split a item from a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.
How can i split an item from a list,lets say list[0] is 12. I want to get the sum of 1 and 2 and replace list[0] with the sum. Which is 3. Any ideas?
A simple function would do the trick. We just have to change the number into a string, turn the string into a list, change the list of strings into a list of numbers, and sum():

def digit_sum(number):
    value = str(number)
    digits = [int(x) for x in value]
    return sum(digits)
Thank you for your reply.I will try to modify this function to work with a list