Problem
Given a dictionary, how can I find out if a given key in that dictionary has already been set to a non-None value?
To put it another way, I’d like to do the following:
my_dict = {}
if (my_dict[key] != None):
my_dict[key] = 1
else:
my_dict[key] += 1
I.e., if there is already a value, I want to increase it; otherwise, I want to set it to 1.
Asked by Ben
Solution #1
You’re looking for collections.defaultdict (Python 2.5 and up). This
from collections import defaultdict
my_dict = defaultdict(int)
my_dict[key] += 1
will carry out your instructions.
If there is no value for a specific key in a conventional Python dict, you will not get None when accessing the dict; instead, a KeyError will be raised. So, instead of your code, if you wish to use a standard dict, you’d use
if key in my_dict:
my_dict[key] += 1
else:
my_dict[key] = 1
Answered by dF.
Solution #2
This is something I prefer to achieve with only one line of code.
my_dict = {}
my_dict[some_key] = my_dict.get(some_key, 0) + 1
The get method in dictionaries accepts two parameters: the key you want and a default value if the key doesn’t exist. This method is preferable to defaultdict because you only want to deal with the circumstance when the key does not exist in this one line of code, not elsewhere.
Answered by Andrew Wilkinson
Solution #3
Personally, I like to use setdefault ()
my_dict = {}
my_dict.setdefault(some_key, 0)
my_dict[some_key] += 1
Answered by kichik
Solution #4
For that, you’ll need the dict idiom key.
if key in my_dict and not (my_dict[key] is None):
# do something
else:
# do something else
You should, however, probably think about using defaultdict (as dF suggested).
Answered by Eli Bendersky
Solution #5
To answer the question “how can I find out if a given index in that dict has already been set to a non-None value”, I would prefer this:
try:
nonNone = my_dict[key] is not None
except KeyError:
nonNone = False
This is consistent with the EAFP concept that has already been mentioned (easier to ask forgiveness then permission). It also prevents a duplicate key lookup in the dictionary, as it would in key in my dict, and my dict[key] is not None, which is useful if lookup is costly.
I also advocate the following for the specific problem you’ve posed, namely, incrementing an int if it exists or setting it to a default value if it doesn’t.
my_dict[key] = my_dict.get(key, default) + 1
as in the answer of Andrew Wilkinson.
If you’re storing modifyable items in your dictionary, there’s a third option. A multimap is a common example of this, in which you store a list of elements for your keys. In that scenario, you can utilise the following:
my_dict.setdefault(key, []).append(item)
If the dictionary doesn’t have a value for key, the setdefault method sets it to the second parameter of setdefault. It returns the value for the key in the same way that a regular my dict[key] does (which may be the newly set value).
Answered by nd.
Post is based on https://stackoverflow.com/questions/473099/check-if-a-given-key-already-exists-in-a-dictionary-and-increment-it