Which method can be used to get the value for a given key from a dictionary, and if the key is not found, it returns a default value?

  • fetch()
  • get()
  • retrieve()
  • value()
The get() method of a dictionary allows you to retrieve the value associated with a given key. If the key is not found, it returns a default value, which can be specified as a second argument to get(). This is a useful way to avoid KeyError exceptions. The other options are not valid methods for this purpose.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *