list = [['03:30', 70.0], ['03:30', 135.0], ['03:30', 200.0], ['03:30', 280.0], ['04:00', 360.0], ['04:00', 430.0], ['04:30', 473.0], ['04:30', 573.0]] 

How to remove items with the same time and leave with a large number to get:

 list = [['03:30', 280.0], ['04:00', 430.0], ['04:30', 573.0]] 

    4 answers 4

     In [49]: from itertools import groupby In [50]: res = [max(g) for _,g in groupby(lst, lambda x: x[0])] In [51]: res Out[51]: [['03:30', 280.0], ['04:00', 430.0], ['04:30', 573.0]] 

    You can explicitly specify a key for the max() function:

     In [53]: res = [max(g, key=lambda x: x[1]) for _,g in groupby(lst, lambda x: x[0])] In [54]: res Out[54]: [['03:30', 280.0], ['04:00', 430.0], ['04:30', 573.0]] 

    This code groups the list of lists by the first element:

     In [57]: for k,g in groupby(lst, lambda x: x[0]): ...: print(k, list(g)) ...: 03:30 [['03:30', 70.0], ['03:30', 135.0], ['03:30', 200.0], ['03:30', 280.0]] 04:00 [['04:00', 360.0], ['04:00', 430.0]] 04:30 [['04:30', 473.0], ['04:30', 573.0]] 

    after that we select in each group an element (list) with a maximum second element:

     In [58]: for k,g in groupby(lst, lambda x: x[0]): ...: print(max(g, key=lambda x: x[1])) ...: ['03:30', 280.0] ['04:00', 430.0] ['04:30', 573.0] 
    • I do not understand how this code works, but it works. Thank! - No Name
    • @NotImeni added a little explanation in reply ... - MaxU
    • @NotImeni works - do not touch. - Pavel Durmanov
    • @Alban, and I didn’t touch as if ...;) - MaxU
    • @MaxU, I missed with the recipient) - Pavel Durmanov

    If we proceed from the fact that your data is sorted by time and value, you can do this:

     from collections import OrderedDict data = [['03:30', 70.0], ['03:30', 135.0], ['03:30', 200.0], ['03:30', 280.0], ['04:00', 360.0], ['04:00', 430.0], ['04:30', 473.0], ['04:30', 573.0]] res = list(OrderedDict(data).items()) print(res) 
    • well, probably, it would be possible to get by with the usual dictionary, however, bravo) - Vasyl Kolomiets

    Well, here's an option without "heavy artillery"

     list = [['03:30', 70.0], ['03:30', 135.0], ['03:30', 200.0], ['03:30', 280.0], ['04:00', 360.0], ['04:00', 430.0], ['04:30', 473.0], ['04:30', 573.0]] list.sort(reverse=True) list2 = [list[0]] for key,value in list: if key != list2[-1][0]: list2.append([key,value]) 

    result in list2.
    In general, it would be possible to write a loop starting from the second element:

     for key,value in list[1:]: ... 

    But it would confuse the eye ...

      Well, if you don’t expect that the data is sorted, then:

       data = [['03:30', 70.0], ['03:30', 135.0], ['03:30', 200.0], ['03:30', 280.0], ['04:00', 360.0], ['04:00', 430.0], ['04:30', 473.0], ['04:30', 573.0]] data.sort(key=lambda x: x[1]) res = list(dict(data).items()) 

      As it works, the list is converted into a dictionary, the feature of which is that the key is unique. This gives the advantage that when you re-enter, we replace it each time with a duplicate one in the list. Actually, for this sorting is needed, so that the last one on the list is the one we need.