user2990084
user2990084

Reputation: 2840

Find max and min date

I have a list with with dates. Currently I am interested only in the key 1 and 2 of each tuple. month/year

search_count=[(1L, 4, 2016), (3L, 5, 2016)]

I am trying this code, but in this way I only get the max and min year. I am interested in the combination of month and year.

min_date = min(search_count,key=itemgetter(2)) 
max_date = max(search_count,key=itemgetter(2))

How can I do that? Seem like i need two keys in min and max.

Upvotes: 4

Views: 269

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52153

You can pass multiple indices into itemgetter. First order by 2 (year), then order by 1 (month):

min_date = min(search_count, key=itemgetter(2, 1))
max_date = max(search_count, key=itemgetter(2, 1))

Upvotes: 5

Related Questions