On Tue, Nov 9, 2010 at 11:00 AM, Matty Sarro <msa...@gmail.com> wrote: > Hey everyone, > I'm working on one of the puzzles on pyschools.com, and am trying to figure > out if I can make my solution a bit more elegant. > > def getSumOfLastDigit(numList): > sumOfDigits=0 > for i in range(0, len(numList)): > num=str(numList.pop()) Just loop over the list directly: for num in numList: num = str(num) > sumOfDigits+=int(num[-1:]) No need for the colon: sumOfDigits+= int(num[-1]) > return sumOfDigits
And now for the much simpler math-based solution: def getSumOfLastDigit(numList): return sum(num%10 for num in numList) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list