I have a list of lists that has items in element 0 and value associated with it in element 1. Each item could appear more than once. I would like to create a list of uniques items with the max value associated with each one. My code accomplishes this, but seems very inefficient. Also, this is a simplified example. mylist could be 100,000 rows. Any suggestions of improving efficiency?
mylist = [['Item 1', 12],['Item 1', 10], ['Item 3', 12],['Item 4', 10], ['Item 3', 14]]
# get unique items
my_unique_items = list(set(x[0] for x in mylist))
# make it a list of list
my_unique_items = [[x] for x in my_unique_items]
# iterate over list items
for item in my_unique_items:
# do list comp to get max value and append
item.append(max([x[1] for x in mylist if x[0] == item[0]]))
print my_unique_items