How to return the sum of the last digits of the values in a list? i cant think of an efficient way. If it wasn't for being in a list i could do it, but im really lost. Can anyone advise me? im going to look for a way in the python library.
Thank you for any help
pwolf 14 Junior Poster in Training
Recommended Answers
Jump to PostIf you think of something else than code under,give an example of list and task.
>>> l = [1,2,3,4] >>> l[:2] [1, 2] >>> l[2:] [3, 4] >>> #Sum up last 2 digits >>> sum(l[2:]) 7
Jump to PostJust to give you a hint with modulo operator as pyTony mention.
>>> 38 % 10 8 >>> 369 % 10 9 >>> 36 % 10 6 >>> 36999 % 10 9 >>>
So loop over element in list with
modulo operator
,and use build in …
All 5 Replies
snippsat 661 Master Poster
pwolf 14 Junior Poster in Training
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
snippsat 661 Master Poster
pwolf 14 Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.