When using the setdefault method on a dictionary, what happens if the provided key already exists in the dictionary?

  • A new key-value pair is added with the default value
  • An error is raised
  • The existing key-value pair is overwritten with the default value
  • The existing value is returned
When setdefault is called with an existing key, it doesn't modify the existing value but returns it. If the key doesn't exist, it adds the key with the provided default value.
Add your answer
Loading...

Leave a comment

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