TNF
TNF

Reputation: 49

Python Removing duplicates ( and not keeping them) in a list

Say I have:

x=[a,b,a,b,c,d]

I want a way to get

y=[c,d]

I have managed to do it with count:

 for i in x:
   if x.count(i) == 1:
     unique.append(i)

The problem is, this is very slow for bigger lists, help?

Upvotes: 4

Views: 90

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

x=["a","b","a","b","c","d"]

from collections import Counter

print([k for k,v in Counter(x).items() if v == 1])    
['c', 'd']

Or to guarantee the order create the Counter dict first then iterate over the x list doing lookups for the values only keeping k's that have a value of 1:

x = ["a","b","a","b","c","d"]
from collections import Counter

cn = Counter(x)
print([k for k in x if cn[k] == 1])

So one pass over x to create the dict and another pass in the comprehension giving you an overall 0(n) solution as opposed to your quadratic approach using count.

The Counter dict counts the occurrences of each element:

In [1]: x = ["a","b","a","b","c","d"]    
In [2]: from collections import Counter    
In [3]: cn = Counter(x)    
In [4]: cn
Out[4]: Counter({'b': 2, 'a': 2, 'c': 1, 'd': 1})
In [5]: cn["a"]
Out[5]: 2  
In [6]: cn["b"]
Out[6]: 2    
In [7]: cn["c"]
Out[7]: 1

Doing cn[k] returns the count for each element so we only end up keeping c and d.

Upvotes: 3

user2426062
user2426062

Reputation: 109

The best way to do this is my using the set() function like this:

x=['a','b','a','b','c','d']
print list(set(x))

As the set() function returns an unordered result. Using the sorted() function, this problem can be solved like so:

x=['a','b','a','b','c','d']
print list(sorted(set(x)))

Upvotes: -2

zw324
zw324

Reputation: 27180

First use a dict to count:

d = {}
for i in x:
    if i not in d:
        d[i] = 0
    d[i] += 1
y = [i for i, j in d.iteritems() if j == 1]

Upvotes: 4

Related Questions