Reputation: 72
I have searched for this but I'm not able to find anything similar to this specific situation:
I have a dict:
nominees = {1931: ['Larry Holmes', 'Jason Russell'],
1932: ['Frank Smith', 'Larry Holmes', 'Jason Russell']}
I'm trying to create a new dict that has the name of each nominee as key, with an int value of how many times they were nominated. Something like this:
nom_count_dict = {'Larry Holmes': 2, 'Jason Russell': 2, 'Frank Smith': 1}
The code that I have so far is wrong. Here is what I have so far:
for year, noms in nominated.items():
for nominee in noms:
if nominee not in nom_count_dict:
nom_count_dict.update({nominee: 1})
elif nominee in nom_count_dict:
nom_count_dict.update({nominee: len(nom_count_dict.keys())})
This is counting the number of total keys within the new dict and adding it onto the value. I know the issue lies here: len(nom_count_dict.keys())
but I can't figure out what to put in len(). Or maybe I shouldn't even be using len()?
Upvotes: 0
Views: 32
Reputation: 56
In your elif clause i.e when you encounter a nominee name, just increment the present nominee count by 1. So your elif clause will look like below
nom_count_dict.update({nominee: nom_count_dict[nominee]+1})
Upvotes: 1