Is there a way to return a default value if a key is not present in a dictionary?
For example, say that I have mydict = {'foo':1, 'bar': 2}
. I want that if I call e.g. :echo mydict['apple']
I get a default value back, e.g. 0
.
Use the get()
function, see :h get()
let mydict = {'foo':1, 'bar': 2}
echo get(mydict, 'apple', 0)