3 ms·
Can you do the same thing with dicts and make it so d['non_existant_key'] does not create an exception?
by brian_herman 6y ago
Can you do the same thing with dicts and make it so d['non_existant_key'] does not create an exception?
- basdftrewq 6y agofrom collections import defaultdict d = defaultdict(int) d['non_existant_key']
- st0le 6y agoNot quite the same what OP asked for. This will create the key and assign value 0 to it.
- lauriat 6y agoYou can already do that with d.get("non_existant_key", default)