I'm trying to get a sum of digits list from an integer list.
In:
list=[12,37,44,96]
Out:
[3,10,8,15]
I have tried tons of methods and havent solved yet.
I'm trying to get a sum of digits list from an integer list.
In:
list=[12,37,44,96]
Out:
[3,10,8,15]
I have tried tons of methods and havent solved yet.
This one-liner should do:
result = [sum(int(digit) for digit in str(number)) for number in lst]
Basically you have to go from int to strings to get the digits and then back to ints to be able to sum them up.
Note: I renamed your list variable to lst to avoid confusion between variable names and keywords.