Find timestamp difference in dictionary values within list of dictionary and capture dictionary with max difference to previous

Question:

I have a list of dictionary like below :

[
    {'dt': '2023-04-08T18:04:33.476+00:00', 'msg': 'VWX'
    },
    {'dt': '2023-04-08T18:04:16.814+00:00', 'msg': 'STU'
    },
    {'dt': '2023-04-08T18:01:40.504+00:00', 'msg': 'PQR'
    },
    {'dt': '2023-04-08T18:01:40.345+00:00', 'msg': 'MNO'
    },
    {'dt': '2023-04-08T18:01:40.068+00:00', 'msg': 'JKL'
    },
    {'dt': '2023-04-08T18:01:39.877+00:00', 'msg': 'DEF'
    },
    {'dt': '2023-04-08T18:01:39.692+00:00', 'msg': 'GHI'
    },
    {'dt': '2023-04-08T18:01:39.579+00:00', 'msg': 'PQR'
    },
    {'dt': '2023-04-08T18:01:39.443+00:00', 'msg': 'XYZ'
    },
    {'dt': '2023-04-08T18:01:24.066+00:00', 'msg': 'ABC'
    }
]

I would like to find time difference between each dictionary and identify the max difference in this list and the corresponding dictionary.
In the above, would like to get ‘STU’ with ~156 secs as output.

With below, I am able to achieve max, however I am a newbie with lambda functions if it can be extended to finding the difference and further to achieve the objective.

maxstatus = max(list, key=lambda x:x['dt'])

Thank you in advance for any expert advice.

Asked By: pats4u

||

Answers:

Assuming the times are already reverse sorted as shown, convert each time to a datetime and compute the difference, remembering the maximum time seen and the index:

import datetime as dt

dlist = [{'dt': '2023-04-08T18:04:33.476+00:00', 'msg': 'VWX'},
         {'dt': '2023-04-08T18:04:16.814+00:00', 'msg': 'STU'},
         {'dt': '2023-04-08T18:01:40.504+00:00', 'msg': 'PQR'},
         {'dt': '2023-04-08T18:01:40.345+00:00', 'msg': 'MNO'},
         {'dt': '2023-04-08T18:01:40.068+00:00', 'msg': 'JKL'},
         {'dt': '2023-04-08T18:01:39.877+00:00', 'msg': 'DEF'},
         {'dt': '2023-04-08T18:01:39.692+00:00', 'msg': 'GHI'},
         {'dt': '2023-04-08T18:01:39.579+00:00', 'msg': 'PQR'},
         {'dt': '2023-04-08T18:01:39.443+00:00', 'msg': 'XYZ'},
         {'dt': '2023-04-08T18:01:24.066+00:00', 'msg': 'ABC'}]

mdiff = dt.timedelta(0)
mi = 0
for i in range(len(dlist) - 1):
    diff = dt.datetime.fromisoformat(dlist[i]['dt']) - dt.datetime.fromisoformat(dlist[i+1]['dt'])
    if diff > mdiff:
        mdiff, mi = diff, i

print(dlist[mi]['msg'], mdiff.total_seconds())

Output:

STU 156.31
Answered By: Mark Tolonen

You can use the max() with a little bit of change

Instead of passing the list as input to max(), passing the generator expression with the calculated differences

from datetime import datetime

res = max(((datetime.fromisoformat(x['dt']) - datetime.fromisoformat(y['dt'])).total_seconds(), x['msg']) for x,  y in zip(data[:-1], data[1:]))
print(*res) # 156.31 STU
Answered By: deadshot
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.