Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sum of value in a dictionary

I’m new here. I wanted to sum all the values inside a dictionary, but my values are all strings, I don’t know how to convert the strings to integers…
I really appreciate if anyone can help with it!

Here’s the dictionary with code

dic1 = dict()
dic1 = {‘2012-03-06’:[‘1′,’4′,’5′],’2012-03-12’:[‘7′,’3′,’10’]}
for i in dic1:
print(i,’,’,sum(dic1[i]))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want the output to be like this:

2012-03-06, 10
2012-03-12, 20

>Solution :

It can be

dic1 = dict() 
dic1 = {'2012-03-06':['1','4','5'],'2012-03-12':['7','3','10']} 
for i in dic1: 
    print(i+','+ str(sum(map(int, dic1[i]))))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading