list deduplication
Method 1: The cyclic deduplication
s= [1,1,2,2,2,3,4,4] res= [] for i in s: if i not in res: res.append(i) print(res) s1= list(set(s)) print(s1)
Method 2: use the dictionary to heavy
res_d ={} for i in s: if i in res_d: res_d[i] += 1 else: res_d[i] =1 print(res_d) for k,v in res_d.items(): print(k)
Method 3: set to heavy, reconverted list
print(list(set(s)))
Method 4: erasing index
s = [1,1,1,2,2,3,4,4] t= s[1:] for i in s: while t.count(i) > 1: del t[t.index(i)] print(t)
Method 5: Using reduce, lambda functions to weight
from functools import reduce l1 = [1,1,1,2,2,3,4,4,4] func = lambda x,y:x if y in x else x+[y] print(reduce(func,[[],]+l1))