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 Values Based on Keys in a Separate Python List

I have a dictionary called hits below.

hits = {"The Weeknd": 6, "Maroon 5": 0, "Justin Bieber": 8, "Post Malone": 5}

I also have a separate, standalone list titled artists:

artists = ["The Weeknd", "Justin Bieber"]

I’d like to sum the total values in the hits dictionary based on the keys listed in the artists list (i.e., answer would be 6 + 8 = 14).

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

>Solution :

sum([v for k,v in hits.items() if k in artists ])

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