Missing dict key
Input
get_with_default({'a': 1}, 'b', 0)
Output
0
Key 'b' is not in the dict, so default 0 is returned.
Full lesson preview
Create a helper that extracts values from mappings or sequences and returns a fallback when the key/index is missing or the container is invalid.
Problem statement
Task
Examples
Input
get_with_default({'a': 1}, 'b', 0)
Output
0
Key 'b' is not in the dict, so default 0 is returned.
Input format
Output format
Constraints
Samples
Input
get_with_default([10, 20], 1, 0)
Output
20
Index 1 exists in the list and returns 20.