Consider the following list of tuples:
transactions = [
('GBP.USD', '2022-04-29'),
('SNOW', '2022-04-26'),
('SHOP', '2022-04-21'),
('GBP.USD', '2022-04-27'),
('MSFT', '2022-04-11'),
('MSFT', '2022-04-21'),
('SHOP', '2022-04-25')
]
I can get the tuple with the minimum date like this:
min(transactions, key=lambda x: x[1])
This returns a single tuple:
('MSFT', '2022-04-11')
I need to return the minimum date of any duplicates along with all unique values. So my output should be this:
[
('SNOW', '2022-04-26'),
('SHOP', '2022-04-21'),
('GBP.USD', '2022-04-27'),
('MSFT', '2022-04-11'),
]
How can I do this?
Solution 1:
min_dates = {}
for item in transactions:
key, date = item
if key not in min_dates or date < min_dates[key]:
min_dates[key] = date
result = [(key, min_dates[key]) for key in min_dates]
result.sort(key=lambda x: x[1])
Solution 2:
from itertools import groupby
# Sort transactions by key and then by date
sorted_transactions = sorted(transactions)
# Group transactions by key
grouped_transactions = groupby(sorted_transactions, key=lambda x: x[0])
# Construct final list of tuples with the minimum date for each key
result = [(key, min(group, key=lambda x: x[1])[1]) for key, group in grouped_transactions]
#print(result)
# Prints : [('GBP.USD', '2022-04-27'), ('MSFT', '2022-04-11'), ('SHOP', '2022-04-21'), ('SNOW', '2022-04-26')]