0

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.

1 Answer 1

2

Use the get() function, see :h get()

let mydict = {'foo':1, 'bar': 2}
echo get(mydict, 'apple', 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.