Skip to main content
Practice

Coding Quiz - Safely Extract Values from a Dictionary

In this coding quiz, you will write a function to safely extract values for specific keys from a dictionary.

When a user selects a key from the dictionary, the function should return the corresponding value or a user-defined default value if the key is not found.

Use the dictionary get method to safely extract values for specific keys.

Function Format
def solution(dictionary, key, default_value):
# Write your code here
return

Constraints

  • The inputs are a dictionary, a key, and a default value.

  • The dictionary keys are in string format.

  • The default value is specified at the time of the function call and should be returned if the key does not exist.


Example Input and Output

  • Input: Dictionary: {"name": "John", "age": 30}, Key: "occupation", Default Value: "Unregistered"

  • Output: "Unregistered"

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.