user2990084 user2990084 - 5 months ago 9x
Python Question

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.


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.


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