-5

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Rhun Rhun
  • 1
  • 2

1 Answers1

0

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.

DocDriven
  • 3,726
  • 6
  • 24
  • 53