Python index of max value in 2D list

counts = [ ["08:00", 50], ["08:15", 47], ["08:30": 66], ["08:45", 52] ]

How do I find the time of the greatest count?

max(zip(*.counts)[1]) will return the value 66.

I'm looking for some version of counts.index() to be return the index so I can retrieve the time (which in the application is a proper datatime datatype) from column 0.

Thanks.

import operator

counts = [ ["08:00", 50], ["08:15", 47], ["08:30", 66], ["08:45", 52] ]

sortedCounts = sorted(counts, key=operator.itemgetter(1))

print sortedCounts

maxItem = sortedCounts[-1]

print maxItem

Output:

[['08:15', 47], ['08:00', 50], ['08:45', 52], ['08:30', 66]]
['08:30', 66]

Or, if you don't care what happens to the original list:

counts = [ ["08:00", 50], ["08:15", 47], ["08:30", 66], ["08:45", 52] ]
print count

counts.sort(key = lambda row:row[1])
print counts

maxItem = counts[-1]
print maxItem

You can also use max:

max(your_list, key=itemgetter(1))
4 Likes