0

I have a list of dictionaries and would like to get the earliest date value (and date only!) from the list. See example below:

{'taps': [{'duration': 0,
   'datetime': '2022-06-05T09:35:56.131498'},
  {'duratin': 518,
   'datetime': '2022-06-05T09:35:56.649846',
  {'duration': 500,
   'datetime': '2022-06-06T09:35:57.150820'}]}

From the example above, I want to get 2022-06-05.

sos.cott
  • 435
  • 3
  • 17
  • 1
    So your question is how to remove the time from the date/time string? (I assume you know how to access the `'taps'` key and how to use the `min` function on the resulting list) – mkrieger1 Oct 19 '22 at 21:10
  • You have a pair of subproblems: (A.) how to extract y-m-d's from datastructure, and (B.) how to compute the min(ymd). But you didn't show us any code. Which subproblem does your current code fail to properly address? https://stackoverflow.com/help/minimal-reproducible-example – J_H Oct 19 '22 at 21:13
  • There's a typo in your dictionary - what have you tried yourself? – Grismar Oct 19 '22 at 21:19

2 Answers2

1

In your example, datetimes are strings only, so this will print the earliest date: (working if strings are all in the format : YYYY-MM-DD...)

items = {
    'taps': [{
        'duration': 0,
        'datetime': '2022-06-05T09:35:56.131498'
    }, {
        'duratin': 518,
        'datetime': '2022-06-05T09:35:56.649846'
    }, {
        'duration': 500,
        'datetime': '2022-06-06T09:35:57.150820'
    }]
}

min(items['taps'],key=lambda x:x['datetime'])['datetime'][:10] # 2022-06-05

If you are working with datetime objects, use this:

items = {
    'taps': [{
        'duration': 0,
        'datetime': datetime(2022, 6, 5, 9, 35, 56, 131498)
    }, {
        'duratin': 518,
        'datetime': datetime(2022, 6, 5, 9, 35, 56, 649846)
    }, {
        'duration': 500,
        'datetime': datetime(2022, 6, 6, 9, 35, 57, 150820)
    }]
}

min(items['taps'], key=lambda x: x['datetime'])['datetime'].strftime('%Y-%m-%d') # 2022-06-05
ErnestBidouille
  • 1,071
  • 4
  • 16
  • Note that this works because the dates happen to be formatted in ISO standard format, which has the date written so that it sorts alphanumerically. For other date formats, a conversion to a datetime format would be required as part of the key function. – Grismar Oct 19 '22 at 21:17
  • Answer edited fir `datetime` formats – ErnestBidouille Oct 19 '22 at 21:24
  • Are you sure ? @RahulKP because `min('2022-06-05', '2022-10-05') == '2022-06-05'` – ErnestBidouille Oct 19 '22 at 21:27
  • @ErnestBidouille Yeah you are right, since all digit prefixes with `0` it seems not to make any issue. I am not sure though. – Rahul K P Oct 19 '22 at 21:31
  • Yeah, only working with strings in ISO format, I agree with you on this point. I really followed his example for my answer – ErnestBidouille Oct 19 '22 at 21:32
  • @ErnestBidouille If the format is different it will make issues, Refer this, https://stackoverflow.com/questions/20365854/comparing-two-date-strings-in-python – Rahul K P Oct 19 '22 at 21:34
  • 1
    @RahulKP Of course, I refer for my answer only to his not very detailed example... – ErnestBidouille Oct 19 '22 at 21:36
1

You can do with dateutil.parser

import dateutil.parser

date_list = sorted([dateutil.parser.isoparse(item['datetime']) for item in d['taps']])
print(date_list[0].strftime("%Y-%m-%d")) # Convert back to string
'2022-06-05'

Conver the into string format datetime to datetime format. Since it's in ISO standard you can use dateutil.parser and sort the list and take the first element.

Rahul K P
  • 15,740
  • 4
  • 35
  • 52