In: Computer Science
python find min in the tuple of dictionaries by date
also how to sort this tuple by effectiveDate
Requests = (
{ "Id": 1, "itemId": 2, "amount": 50, "effectiveDate": "11/15/2015"},
{ "Id": 2, "itemId": 1, "amount": 200, "effectiveDate": "11/20/2015"},
{ "Id": 3, "itemId": 3, "amount": 5000, "effectiveDate": "10/5/2015"},
{ "Id": 4, "itemId": 3, "amount": 600, "effectiveDate": "10/6/2015"}
)
Hi..
-------------------------------------------------------------
working code: find min by effectiveDate
Requests = (
{ "Id": 1, "itemId": 2, "amount": 50, "effectiveDate": "11/15/2015"},
{ "Id": 2, "itemId": 1, "amount": 200, "effectiveDate": "11/20/2015"},
{ "Id": 3, "itemId": 3, "amount": 5000, "effectiveDate": "10/5/2015"},
{ "Id": 4, "itemId": 3, "amount": 600, "effectiveDate": "10/6/2015"})
key_min = min(Requests,key=lambda x:x['effectiveDate'])
key_min
This is screenshot of above code and its output
------------------------------------------------------
Sort the dictionary by effectiveDate
Requests = (
{ "Id": 1, "itemId": 2, "amount": 50, "effectiveDate": "11/15/2015"},
{ "Id": 2, "itemId": 1, "amount": 200, "effectiveDate": "11/20/2015"},
{ "Id": 3, "itemId": 3, "amount": 5000, "effectiveDate": "10/5/2015"},
{ "Id": 4, "itemId": 3, "amount": 600, "effectiveDate": "10/6/2015"})
print("sorted List",sorted(Requests,key=lambda x:x['effectiveDate']))
This is screenshot of above code and its output
Still if you have any doubt or need any kind improvement, please let me know in comment section and if you like, Please upvote.
Thank You !