1

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).

1
  • 5
    You just want something like sum(hits[artist] for artist in artists) Commented Jun 19, 2022 at 6:10

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I see sum(hits[artist] for artist in artists) also works, which looks to be more efficient/easy on the eyes. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.