I'm trying to write a code that checks if a specific value in a list that contains dictionaries and/or lists is present, and it returns True or False.
Note: I cannot change the structure of the data.
I manage to have this function that does the job for the current data type that I have:
def search_value_list_dic(value, list_dic):
if any(isinstance(el, list) for el in list_dic):
number_lists = len(list_dic)
list_or = []
for i in range(number_lists):
if value in list_dic[i]:
list_or.append('true')
else:
list_or.append('false')
if 'true' in list_or: return True
else: return False
if value in list_dic:
return True
if value not in list_dic:
return False
I'm aware that this is not the most efficient way of doing it. Does anyone have any ideas on how to improve?
I also would like to make it more generic, basically to search for every item on the list (even if it has list on list on list and so on).
Example of data structure:
[{'id': 'BuMnLoadRequestCfgBufferRequestOnHysteresis', 'view_id': 122, 'label': {'key': 'BUMN_LOAD_REQUEST_CFGBUFFERREQUESTONHYSTERESIS', 'en': 'Hysteresis to start buffer cylinder loading', 'de': 'Einschalthysterese Puffer'}, 'range': None, 'testing_conditions': [[{'ordName': 'BuMnLoadRequestMnIsActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.mnIsActive.1 == true', 'condition_testing': 'BuMn.load.request.mnIsActive.1 == 1'}, 'OR', {'ordName': 'BuMnLoadHydMnIsFlowControlActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.hyd.mnIsFlowControlActive.1 == true', 'condition_testing': 'BuMn.load.hyd.mnIsFlowControlActive.1 == 1'}], {'ordName': 'BuMnLoadRequestInAHSHydraulic', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.inAHSHydraulic.4 == BUFFER', 'condition_testing': 'BuMn.load.request.inAHSHydraulic.4 == 1'}], 'type': 'customkeypadonly_text_linear', 'user_level': 3}, {'id': 'BuMnLoadRequestCfgBufferRequestOffHysteresis', 'view_id': 122, 'label': {'key': 'BUMN_LOAD_REQUEST_CFGBUFFERREQUESTOFFHYSTERESIS', 'en': 'Hysteresis to stop buffer cylinder loading', 'de': 'Ausschalthysterese Puffer'}, 'range': None, 'testing_conditions': [[{'ordName': 'BuMnLoadRequestMnIsActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.mnIsActive.1 == true', 'condition_testing': 'BuMn.load.request.mnIsActive.1 == 1'}, 'OR', {'ordName': 'BuMnLoadHydMnIsFlowControlActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.hyd.mnIsFlowControlActive.1 == true', 'condition_testing': 'BuMn.load.hyd.mnIsFlowControlActive.1 == 1'}], {'ordName': 'BuMnLoadRequestInAHSHydraulic', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.inAHSHydraulic.4 == BUFFER', 'condition_testing': 'BuMn.load.request.inAHSHydraulic.4 == 1'}], 'type': 'customkeypadonly_text_linear', 'user_level': 3}, {'id': 'BuMnLoadRequestCfgBufferAHSOffset', 'view_id': 122, 'label': {'key': 'BUMN_LOAD_REQUEST_CFGBUFFERAHSOFFSET', 'en': 'Buffer to heat source set temperature increase', 'de': 'Temperaturanhebung Puffer zu Mischer/Wärmeerzeuger'}, 'range': None, 'testing_conditions': [{'ordName': 'BuMnLoadRequestMnIsActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.mnIsActive.1 == true', 'condition_testing': 'BuMn.load.request.mnIsActive.1 == 1'}, {'ordName': 'BuMnLoadRequestInAHSHydraulic', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.inAHSHydraulic.4 == BUFFER', 'condition_testing': 'BuMn.load.request.inAHSHydraulic.4 == 1'}], 'type': 'customkeypadonly_text_linear', 'user_level': 2}]
And the value I want to search: OR
Thanks