Search code examples
pythonpython-3.xlistloopsnested-lists

How to delete the dulplicate list in list of list if I want to check by spetific index?


So I try to delete the duplicate list in list of list by checking just from only one element in that list. I only know to delete the duplicate from normal list, can you guys help me? Thanks!

So what I want to do is append just ONLY one list per name which I have sort it and the most value of each person will be the first one of another value in the same name.

input : liz = [['Lina', 60], ['Gaga', 0], ['Dene', 100], ['Dene', 80], ['Dene', 0], ['Helen', 0], ['Will', 100]] expected output : lix = [['Lina', 60], ['Gaga', 0], ['Dene', 100], ['Helen', 0], ['Will', 100]]

What I have try to reach it and it just append all of list into list because it isn't equal to recent of all because its value of index[1]:

lix = []
for ele in liz:
        if ele not in lix:
            lix.append(ele)

Solution

  • To do so, you can simply have a record of all the names that are already in the list and compare them.

    lix = {} # use dict instead of list
    for pair in liz:
        if pair[0] in lix:
            if lix[pair[0]] < pair[1]: # if it is not the largest, add the largest
                lix[pair[0]] = pair[1]
        else:
            lix[pair[0]] = pair[1]