From a list like :
mylist = [{'x':2020 , 'y':20},{'x':2020 , 'y':30},{'x':2021 , 'y':10},{'x':2021 , 'y':5}]
I want to keep all 'x' unique and 'y' to be the maximum where 'x' is the same.
I am trying to get the output as:
mylist_unique = [{'x':2020 , 'y':30},{'x':2021 , 'y':10}]
I have implemented it in a very naive way:
res =[]
temp = {}
print(len(temp))
for i in range(len(mylist)):
print(mylist[i])
for k,v in mylist[i].items():
print(mylist[i]['x'],temp.keys(),mylist[i]['y'])
if mylist[i]['x'] not in temp.keys() or mylist[i]['y'] > (temp[mylist[i]['x']]) :
print(k)
temp.update({mylist[i]['x']:mylist[i]['y']})
print(temp)
for k,v in temp.items():
res.append({'x':k,'y':v})
print(res)