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 ...?
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.
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
the two "for" show possible solutions if you don't care about key or if you do...
Thanks @Shadowys and @FabienToune. I'll try your methods and let you know..
Join our real-time social learning platform and learn together with your friends!