Junior — Senior
Extracting a value from a nested dictionary or list by dot path
livecode
Task condition
Implement the function get_by_path(obj: dict | list, path: str) -> Any, which takes:
- obj – an arbitrary nested dictionary or list;
- path – a string describing a sequence of keys/indices separated by dots.
The function should traverse the specified path and return the found value. If at any point the path becomes inaccessible (missing key, incorrect data type, list index out of range, etc.), it should return None.
Example usage:
obj = {
'a': {
'b': {
'c': 1
}
}
}
get_by_path(obj, 'a.b.c') # returns 1
get_by_path(obj, 'a.d.e') # returns None
Starter code for the function:
from typing import Any
def get_by_path(obj: dict | list, path: str) -> Any:
...