forked from abhijeet-reddy/Competitive_Programming_Score_API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
17 lines (15 loc) · 605 Bytes
/
util.py
File metadata and controls
17 lines (15 loc) · 605 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def get_safe_nested_key(keys, dictionary):
if not isinstance(dictionary, dict):
return None
if isinstance(keys, str):
return dictionary.get(keys)
if isinstance(keys, list):
if len(keys) == 1:
return dictionary.get(keys[0])
if len(keys) > 1:
return get_safe_nested_key(keys[1:], dictionary.get(keys[0]))
return None
return None
if __name__ == '__main__':
assert get_safe_nested_key(['a', 'b', 'c'], {"a": {"b": {"c": "C"}}}) == "C"
assert get_safe_nested_key(['a', 'b', 'c'], {"a": "A", "b": "B", "c": "C"}) is None