Lecture
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"
Lessons in this chapter · Dictionary and Set Data Types for Managing Structured Data
- 1. Managing Structured Data with Dictionaries
- 2. Utilizing Values Within a Dictionary
- 3. Adding and Modifying Elements in a Dictionary
- 4. Removing a Specific Element from a Dictionary
- 5. Multiple-choice quiz
- 6. Handling Dictionary KeyError Exceptions
- 7. Checking Key Existence in a Dictionary
- 8. Safely Checking for Key Existence in a Dictionary
- 9. Multiple-choice quiz
- 10. Set Data Type that Does Not Allow Duplicate Values
- 11. Intersection, Union, Difference Operations
- 12. Adding Elements to a Set
- 13. Removing Elements from a Set
- 14. Utilizing Values within a Set
- 15. Differences Between Shallow and Deep Copy
- 16. Coding Quiz - Safely Extract Values from a Dictionary
- 17. Multiple-choice quiz
- 18. Fill-in-the-blank quiz
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help