Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get minimum unique tuple pair from list of tuples

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

('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 :

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])

enter image description here

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')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading