alwbtc
alwbtc

Reputation: 29445

How to get the item with max date value in a dictionary with mixed type values?

In the dictionary a:

from datetime import datetime

a = {"ID":3, "CITY":"ATLANTIS", "FOUNDATION":datetime(2014,10,12), "COLLAPSE":datetime(2010,10,12), "REGENERATE":datetime(2011,10,12)}

How would you get the value which has the oldest date in this dictionary (in this case "COLLAPSE":datetime(2010,10,12))? Remember, not all values are of same data type.

Upvotes: 4

Views: 6632

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121406

If you wanted the earliest date, you want to use min(), not max() here; a datetime earlier in time sorts before another that is later.

You'd need to use a custom key callable here, and return datetime.max for any value that is not a datetime object to discount that key:

from datetime import datetime

min(a.iteritems(),
    key=lambda i: i[1] if isinstance(i[1], datetime) else datetime.max)

This returns both the key and the value.

To get just the key, use:

min(a, key=lambda k: a[k] if isinstance(a[k], datetime) else datetime.max)

and for just the value:

min(a.itervalues(),
    key=lambda v: v if isinstance(v, datetime) else datetime.max)

In all cases only the exact syntax to get the value to compare differs.

Demo:

>>> from datetime import datetime
>>> a = {"ID":3, "CITY":"ATLANTIS", "FOUNDATION":datetime(2014,10,12), "COLLAPSE":datetime(2010,10,12), "REGENERATE":datetime(2011,10,12)}
>>> min(a.iteritems(),
...     key=lambda i: i[1] if isinstance(i[1], datetime) else datetime.max)
('COLLAPSE', datetime.datetime(2010, 10, 12, 0, 0))
>>> min(a, key=lambda k: a[k] if isinstance(a[k], datetime) else datetime.max)
'COLLAPSE'
>>> min(a.itervalues(),
...     key=lambda v: v if isinstance(v, datetime) else datetime.max)
datetime.datetime(2010, 10, 12, 0, 0)

Upvotes: 5

Related Questions