'How to extract from the list of dictionary by checking latest timestamp and value
There is a list of dictionaries below like
my_dictionary = [
{
'name': 'Tester',
'id': '101',
'status': 'Failed',
'lastModifiedDate': '2022-02-14 12:25:32:013302' },
{
'name': 'Developer',
'id': '102',
'status': 'Success',
'lastModifiedDate': '2022-02-14 12:25:32:013302',
},
{
'name': 'Tester',
'id': '101',
'status': 'Failed',
'lastModifiedDate': '2022-02-21 12:25:32:013302' }
]
- I have two
namevalues asTesterorDeveloper - By checking latest
lastModifiedDateneed to extract latest entry forTester,Developer
Logic implemented in the below code is as follows:
- First sort the on the basis of
lastModifiedDatein descending order. - Extract the first occurrence in the list of dictionaries.
response = sorted(my_dictionary, key=lambda x: x['lastModifiedDate'], reverse=True)
response_latest = []
for item in response:
extracted_response = {}
for field in item:
if item['name'] == 'Tester':
extracted_response[field] = item[field]
response_latest.append(extracted_response)
break
for field in item:
if item['name'] == 'Developer':
extracted_response[field] = item[field]
response_latest.append(extracted_response)
break
response_latest
The output of the above code on input as the above list of the dictionaries is
[{'name': 'Tester',
'id': '101',
'status': 'Failed',
'lastModifiedDate': '2022-02-21 12:25:32:013302'}]
but the expected output is
[{'name': 'Tester',
'id': '101',
'status': 'Failed',
'lastModifiedDate': '2022-02-21 12:25:32:013302'},
{'name': 'Developer',
'id': '102',
'status': 'Success',
'lastModifiedDate': '2022-02-14 12:25:32:013302'}]
Also, code should not fail in case of the absence of Developer or Tester dictionary name values.
my_dictionary = [{'name': 'Tester', 'id': '101', 'status': 'Failed', 'lastModifiedDate': '2022-02-14 12:25:32:013302'},
{'name': 'Tester', 'id': '101', 'status': 'Failed', 'lastModifiedDate': '2022-02-21 12:25:32:013302'}]
Solution 1:[1]
If pandas is in play:
import pandas as pd
df = pd.DataFrame(myd)
maxes = df.groupby('name')['lastModifiedDate'].transform('max')
result = df[df['lastModifiedDate'] == maxes].to_dict(orient='records')
Output:
>>> result
[{'name': 'Developer', 'id': '102', 'status': 'Success', 'lastModifiedDate': '2022-02-14 12:25:32:013302'}, {'name': 'Tester', 'id': '101', 'status': 'Failed', 'lastModifiedDate': '2022-02-21 12:25:32:013302'}]
Solution 2:[2]
You can try the method of converting a list of dictionaries to a pandas data frame and perform a few actions on it.
This example includes two Tester dictionaries having the same lastModifiedDate but having different lastModifiedDate in the case of Developer
my_dictionary_list = [
{
'name': 'Tester',
'id': '101',
'status': 'Failed',
'lastModifiedDate': '2022-02-14 12:25:32:013302'},
{
'name': 'Developer',
'id': '102',
'status': 'Success',
'lastModifiedDate': '2022-02-14 12:25:32:129405'},
{
'name': 'Tester',
'id': '103',
'status': 'Failed',
'lastModifiedDate': '2022-02-14 12:25:32:013302'},
{
'name': 'Developer',
'id': '102',
'status': 'Success',
'lastModifiedDate': '2022-02-21 12:25:32:113215'},
{
'name': 'Tester',
'id': '103',
'status': 'Failed',
'lastModifiedDate': '2022-02-12 12:25:32:013302'},
]
import pandas as pd
df = pd.DataFrame(my_dictionary_list)
maxes_by_group = df.groupby(['name'])['lastModifiedDate'].transform(max)
print("maxes_by_group output - \n{}".format(maxes_by_group))
result = df[df['lastModifiedDate'] == maxes_by_group].to_dict(orient='records')
print("--------------------")
print("Final result output - \n{}".format(result))
Output -
maxes_by_group output -
0 2022-02-14 12:25:32:013302
1 2022-02-21 12:25:32:113215
2 2022-02-14 12:25:32:013302
3 2022-02-21 12:25:32:113215
4 2022-02-14 12:25:32:013302
Name: lastModifiedDate, dtype: object
--------------------
Final result output -
[{'name': 'Tester', 'id': '101', 'status': 'Failed', 'lastModifiedDate': '2022-02-14 12:25:32:013302'}, {'name': 'Tester', 'id': '103', 'status': 'Failed', 'lastModifiedDate': '2022-02-14 12:25:32:013302'}, {'name': 'Developer', 'id': '102', 'status': 'Success', 'lastModifiedDate': '2022-02-21 12:25:32:113215'}]
maxes_by_group variable output represents descending order sorting of lastModifiedDate values.
You can see three dictionaries in the final result variable output. Two Tester name values are having the same maximum lastModifiedDate values and one Developer name value is having the maximum lastModifiedDate value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | timgeb |
| Solution 2 |
