looking for a more robust method to access an element in a nested dictionary using MainKey (which can be at any depth), SubKey (always immediately following MainKey), and Element (following SubKey at any depth). While my current implementation works for contiguous MainKey, SubKey, and Element, it struggles when they're not directly adjacent. Any suggestions on how to improve this?
Nested_Dict =
{'candela_samples_generic': {'drc_dtcs': {'domain_name': 'TEMPLATE-DOMAIN', 'dtc_all': {'0x930001': {'identification': {'udsDtcValue': '0x9300', 'fault_type': '0x11', 'description': 'GNSS antenna short to ground'}, 'snapshots': {'snapshot_record_content': 'base', 'snapshot_records_numbers': ['0x01']}, 'functional_conditions': {'failure_name': 'short_to_ground', 'mnemonic': 'DTC_GNSS_Antenna_Short_to_ground'}}}}}}`
Working for result = get_value_nested_dict(nested_dict, 'dtc_all', '0x930001', 'identification') not for result = get_value_nested_dict(nested_dict, 'dtc_all', '0x930001', 'udsDtcValue')
def get_value_nested_dict(nested_dict, main_key, sub_key, element):
# Initialize a list to store the results
results = []
# Recursive function to search for the element in nested dictionaries
def search_nested_dict(nested_dict, main_key, sub_key, element):
# Iterate over the dictionary items
for key, value in nested_dict.items():
# Check if the current key matches the main key
if key == main_key:
# Check if the sub_key exists in the value
if sub_key in value:
# Check if the element exists directly under the sub_key
if element in value[sub_key]:
results.append(value[sub_key][element])
# If not, recursively search for the element in the sub-dictionary
elif isinstance(value[sub_key], dict):
search_nested_dict(value[sub_key], main_key, sub_key, element)
# If the value is a dictionary, recursively call the function
elif isinstance(value, dict):
search_nested_dict(value, main_key, sub_key, element)
# Start the recursive search
search_nested_dict(nested_dict, main_key, sub_key, element)
return results
To optimize the function you should adjust your strategy:
MainKey
.MainKey
is found, right away look for SubKey
within the part of the dictionary where MainKey
was found.SubKey
, we search everything under SubKey
to find Element
.The optimized version:
def get_value_nested_dict(nested_dict, main_key, sub_key, element):
results = []
def search_for_element(sub_dict, looking_for_element):
if isinstance(sub_dict, dict):
for k, v in sub_dict.items():
if k == element and looking_for_element:
results.append(v)
else:
search_for_element(v, looking_for_element)
def search_nested_dict(current_dict):
if isinstance(current_dict, dict):
for key, value in current_dict.items():
if key == main_key:
if sub_key in value:
search_for_element(value[sub_key], True)
else:
search_for_element(value, False)
else:
search_nested_dict(value)
search_nested_dict(nested_dict)
return results