python nested list format, search for values greater than or latest

Below is in which I have data.

 a =   [{'ID': 319684283, 'ID1': 1025018, 'date': '2018-01-07 17:39:46', 'rate': 9.639e-05, 'amount': 410.84392747, 'total': 0.03960124, 'order': 16532584965, 'type': 'A', 'category': 'website'}, {'ID': 319684282, 'ID1': 1025017, 'date': '2018-01-07 17:39:46', 'amount': 24.84386425, 'total': 0.00239445, 'order': 16532584965, 'type': 'phone', 'category': 'exchange'}, {'ID': 319684281, 'ID1': 1125117, 'date': '2018-01-17 17:39:16', 'amount': 14.8138145, 'total': 0.10239445, 'order': 16512581965, 'type': 'phone', 'category': 'exchange'}]

How do I pull record which is latest of these. I tried

for c in a:
   print(min(c['date']

It failed as it is not able to traverse all the elements. How do I achieve the below out put, limited values:

d = { 'date': '2018-01-17 17:39:16', 'amount': 14.8138145, 'total': 0.10239445, 'order': 16512581965} 

Here d should have only the latest value based on date.

1 answer

  • answered 2018-01-11 20:13 Gaurang Shah

    you need to iterate through each list and then from dictionary need to get the date to find the minimum date. Once you have find the minimum date you will have your value.

    min_date = "2222-01-01 00:00:00"
    for dict in data_list:
        date = dict.get("date")
        if date < min_date:
            value = dict