[Python] comparing elements of a list of list and removing

Discussão em 'Python' iniciado por Stack, Julho 28, 2025.

  1. Stack

    Stack Membro Participativo

    l=[['a', 'random_str', 4], ['b', 'random_str2', 5], ['b', 'random_str3', 7]]


    so I have a list like this and I want to traverse through this list to check if the zeroth index of each sublist is equivalent to one another so check if any zeroth index of each sublist is equal to another, and then if two of or more are similar check the second index in the sublist and only keep the sublist with lowest int value and remove all others.

    so the output should be that

    [['a', 'random_str', 4], ['b', 'random_str2', 5]]


    so it removes the sublist with the higher int in the second index

    I'm thinking something like this

    for i in l:
    for k in i:
    if k[0]=i[0][0]:
    # then I dont know

    Continue reading...

Compartilhe esta Página