Given a dictionary, how can I find out if a given key in that dictionary has already been set to a non-None value?

I.e., I want to do this:

my_dict = {}

if (my_dict[key] != None):
  my_dict[key] = 1
else:
  my_dict[key] += 1

I.e., I want to increment the value if there’s already one there, or set it to 1 otherwise.

12 Answers
12

Leave a Reply

Your email address will not be published. Required fields are marked *