[ACCEPTED]-Removing duplicates from list of lists in Python-list
Accepted answer
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))
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)
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])
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
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
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.