How to get last received value from dynamic list of dictionaries

60 Views Asked by At

I pass a list of dictionaries (which varies dynamically based on robot framework test execution) to a python function to extract kPassed and kFailed. How to fetch the latest value of key in mycase 'called_by' and proceed with extraction function?

For example : at the end of three robot test steps executions, list_of_dictionaries contains three value, in that key with timestamp': 3 is the newest addition to the list ,I want to take only that latest value from the list for remaining extraction.

Suggestion are appreciated.I'm new to python.

list_of_dictionaries = [
    {'called_by': 'monitoraction', 'arguments': {'action': 'kPassed'},'timestamp': 1},
    {'called_by': 'monitoraction', 'arguments': {'action': 'kPassed'},'timestamp': 2}
    {'called_by': 'monitoraction', 'arguments': {'action': 'kFailed'},'timestamp': 3}]

def extract_kPassed(list_of_dictionaries: list):
    retVal = []
    for item in list_of_dictionaries:
        try:
            if item['called_by'] == 'monitoraction':
                if item['arguments'].get('action', None) == 'kPassed':
                    retVal.append(item['arguments'].get('action', None))             
                    retStr = ''.join(retVal)
        except:
            pass
    return retStr

def extract_DTC_kFailed(list_of_dictionaries: list):
    retVal = []
    for item in list_of_dictionaries:
        try:
            if item['called_by'] == 'monitoraction':
                if item['arguments'].get('action', None) == 'kFailed':
                    retVal.append(item['arguments'].get('action', None))                    
                    retStr = ''.join(retVal)
        except:
            pass
    return retStr
0

There are 0 best solutions below