st_bones
st_bones

Reputation: 129

max value from nested list/dictionary - Python

How to do i get this below result from a nested dictionary list?

data_list = [{'number': 1, 'category': 'orange', 'timestamp': '2020-09-17-10:29:40'}, {'number': 1, 'category': 'peach', 'timestamp': '2019-08-12-10:29:40'}]

I want the item in list has Max timestamp, hence below is the result i am expecting. expected result = {'number': 1, 'category': 'orange', 'timestamp': '2020-09-17-10:29:40'}

Upvotes: 2

Views: 433

Answers (1)

soulmerge
soulmerge

Reputation: 75744

Python's max() function accepts a keyword-only argument key, that defines how the "highest" value should be determined. You can use that to compare the parsed timestamp like this:

max(values, key=lambda item: datetime.strptime(item['timestamp'], '%y-%m-%d-%H:%M:%S'))

Upvotes: 2

Related Questions