sim
sim

Reputation: 488

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'  }
]

Logic implemented in the below code is as follows:

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'}]

Upvotes: 3

Views: 781

Answers (3)

Shubhank Gupta
Shubhank Gupta

Reputation: 825

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.

Upvotes: 0

Leonardo Tamayose
Leonardo Tamayose

Reputation: 24

I tried filtering myd and then finding the latest of each one.

tester = list(filter(lambda x: x["name"] == "Tester", myd))
developer = list(filter(lambda x: x["name"] == "Developer", myd))

tester_latest = sorted(tester, key=lambda x: x["lastModifiedDate"], reverse=True)
developer_lastest = sorted(developer, key=lambda x: x["lastModifiedDate"], reverse=True)

response_latest = [
    tester_latest[0] if len(tester_latest) > 0 else None,
    developer_lastest[0] if len(developer_lastest) > 0 else None
]
response_latest = list(filter(None, response_latest)
response_latest

Upvotes: 0

timgeb
timgeb

Reputation: 78690

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'}]

Upvotes: 1

Related Questions