Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (velumani):

In Python, I have a dictionary with strings as keys, and strings or list of number as the associated values. How to do arithmetic operations like sum() on the list of numbers using for-in loop statement on the dictionary ...?

OpenStudy (anonymous):

You might want to try this. i=0 for x in range(0,len(list)): i += list[x] print (i) though I'm all out for a more elegant one.

OpenStudy (anonymous):

weird... i've answered to this one this morning, then it disapeared... d = { "monday" : [25, 22], "tuesday" : "nothing", "wednesday" : [9, 8, 7], "thursday" : [30, 12] } for v in d.values(): if type(v)==list: print sum(v) for k, v in d.items(): if type(v)==list: print k, ":", sum(v) else: print k, ":", v

OpenStudy (anonymous):

the two "for" show possible solutions if you don't care about key or if you do...

OpenStudy (velumani):

Thanks @Shadowys and @FabienToune. I'll try your methods and let you know..

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!