[ACCEPTED]-Removing duplicates from list of lists in Python-list

Accepted answer
Score: 32

Do you care about preserving order / which 3 duplicate is removed? If not, then:

dict((x[0], x) for x in L).values()

will 2 do it. If you want to preserve order, and 1 want to keep the first one you find then:

def unique_items(L):
    found = set()
    for item in L:
        if item[0] not in found:
            yield item
            found.add(item[0])

print list(unique_items(L))
Score: 4

use a dict instead like so:

L = {'14': ['65', 76], '2': ['5', 6], '7': ['12', 33]}
L['14'] = ['22', 46]

if you are receiving 2 the first list from some external source, convert 1 it like so:

L = [['14', '65', 76], ['2', '5', 6], ['7', '12', 33], ['14', '22', 46]]
L_dict = dict((x[0], x[1:]) for x in L)
Score: 2

Use Pandas :

import pandas as pd

L = [['14', '65', 76], ['2', '5', 6], ['7', '12', 33], ['14', '22', 46],['7','a','b']]

df = pd.DataFrame(L)
df = df.drop_duplicates()

L_no_duplicates = df.values.tolist()

If you want to drop duplicates 1 in specific columns only use instead:

df = df.drop_duplicates([1,2])
Score: 0

i am not sure what you meant by "another 2 list", so i assume you are saying those 1 lists inside L

a=[]
L = [['14', '65', 76], ['2', '5', 6], ['7', '12', 33], ['14', '22', 46],['7','a','b']]
for item in L:
    if not item[0] in a:
        a.append(item[0])
        print item
Score: 0

If the order does not matter, code below

print [ [k] + v for (k, v) in dict( [ [a[0], a[1:]] for a in reversed(L) ] ).items() ]

gives

[['2', '5', '6'], ['14', '65', '76'], ['7', '12', '33']]

0

More Related questions