I usually use the following idiom when working with a Python dictionary:
try:
val = dct[key]
except KeyError:
print key, " is not valid"
since for large dictionaries, the statement
if key in dct:
# do something
is not very efficient (so I remember reading, but I've noticed it in practice as well)
Today I was working with a defaultdict and for a moment I forgot that a defaultdict will never give you a KeyError but instead will update the original dictionary.
How can I perform a lookup without updating the defaultdict? I really need to print an error so that the user can reenter the key.
Thank you!
UPDATE: Several posters suggested that my belief that if key in dct:
is slow is false. I went back and checked the book in which I had read that is better to use try: except:
. It is 2002's Python Cookbook, Recipe 1.4 by Alex Martelli, which can be found also online here: Add an entry to dictionary. Old memories are so unreliable! The recipe doesn't mention "slower" and it's not even using in
but has_key
. It simply says that try: except:
is more Pythonic (at least the book version of the recipe). Thanks for the correction and the answers.
How can I perform a lookup without updating the defaultdict?
With key in dct
, i.e. explicitly.
If this is really too expensive for you (measure and you'll be sure), there are workarounds for specific situations. E.g., if your default value is 'ham'
and in some situations you don't want to store (key, 'ham')
in the defaultdict
when key
is not found, you can do
dct.get(key, 'ham') # will return dct[key] or 'ham' but never stores anything