Reputation: 725
I have the following list of dictionaries in python:
data = [{'flag': 'one', 'timestamp': 20190710},
{'flag': 'one', 'timestamp': 20190711},
{'flag': 'two', 'timestamp': 20190712},
{'flag': 'two', 'timestamp': 20190709}]
I would like to return the dictionary with the the highest value in the key timestamp
and with the value one
in the key flag
.
I have done something similar here:
Return the key of the maximum value in a dictionary base on criteria in Python
But cannot make it work in this new set-up.
Upvotes: 2
Views: 223
Reputation: 195543
You could use max()
built-in function with custom key=
. Before that, we filter the dictionary only for items with key 'flag'
== 'one'
:
data = [{'flag': 'one', 'timestamp': 20190710},
{'flag': 'one', 'timestamp': 20190711},
{'flag': 'two', 'timestamp': 20190712},
{'flag': 'two', 'timestamp': 20190709}]
print(max((i for i in data if i['flag'] == 'one'), key=lambda k: k['timestamp']))
Prints:
{'flag': 'one', 'timestamp': 20190711}
EDIT: Quick benchmark of what's faster - doing the filtration first or finding the max in whole list:
from random import choice,randint
data = []
for i in range(10000):
data.append({'flag': choice(['one', 'two']), 'timestamp': randint(1, 1_000_000)})
def fn1():
return max(data, key=lambda x: (x["flag"] == 'one', x["timestamp"]))
def fn2():
return max((i for i in data if i['flag'] == 'one'), key=lambda k: k['timestamp'])
from timeit import timeit
t1 = timeit(lambda: fn1(), number=1_000)
t2 = timeit(lambda: fn2(), number=1_000)
print(t1)
print(t2)
Prints:
1.5405012619994523
0.8562619980002637
Which means doing the filtration first is faster.
Upvotes: 3
Reputation: 82785
Using max
with custom key without iteration.
Ex:
data = [{'flag': 'one', 'timestamp': 20190710},{'flag': 'one', 'timestamp': 20190711},{'flag': 'two', 'timestamp': 20190712}, {'flag': 'two', 'timestamp': 20190709}]
print(max(data, key=lambda x: (x["flag"] == 'one', x["timestamp"])))
Output:
{'flag': 'one', 'timestamp': 20190711}
Upvotes: 1