In Clojure you can update the map (dict) with assoc-inand automatically create a key path if it does not exist.
(assoc-in {:a 1 :b 3} [:c :d] 33)
{:a 1, :c {:d 33}, :b 3}
The same for get-in: you can specify the path to the keys (or list indexes), and it will return the value specified in the path nil, if it does not exist.
(get-in {:a 1, :c {:d 33}, :b 3} [:c :d])
33
(get-in {:a 1, :c {:d 33}, :b 3} [:c :e])
nil
Is there a Python equivalent or comparable shortcut out of the box? (yes, I know that I can write deceptive dictations, but I would like to avoid this).
source
share