Hacker News new | ask | show | jobs
by brian_herman 1992 days ago
Can you do the same thing with dicts and make it so d['non_existant_key'] does not create an exception?
2 comments

    from collections import defaultdict

    d = defaultdict(int)

    d['non_existant_key']
Not quite the same what OP asked for. This will create the key and assign value 0 to it.
You can already do that with

  d.get("non_existant_key", default)