modulitos
modulitos

Reputation: 15814

Python 2.7: Get all unique values from a list, removing any value that is or has been repeated

What is a reasonable way to get only the unique values from a list in Python 2.7? For example, if a value is repeated, delete the repeat AND the original value from the list. Thus, we are left with only the values that have never been repeated.

Intuitively, I would create a set from the list, and a bag (ie multiset) from the list, and take the values from the list that are NOT in the bag MINUS set collection. However, I cannot find an easy implementation for bag (or multiset) in Python 2.7. Any suggestions? It would be nice to implement this using only set and bag operations.

Example Here a my best implementation, using a brute force approach:

This example uses a list of friends, where if a friend is listed more than once, they are no longer a friend:

list_of_friends = ['bill','bill','mark','jenna','brad','mark']
unique_list_of_friends = []
for friend in list_of_friends:
    if (friend in list_of_friends):
        list_of_friends.remove(friend)
        if (friend not in list_of_friends):
            unique_list_of_friends.append(friend)
        # Ensure that all duplicates of current friend are removed
        list_of_friends = [f for f in list_of_friends if f != friend]

Finally, we are left with:

unique_list_of_friends = ['jenna','brad']

Upvotes: 0

Views: 865

Answers (10)

mukul bahuguna
mukul bahuguna

Reputation: 11

a = [1,2,3,4,5,6,6,5,4,3,2,1,1,12,4,6,2,3,7,9,4,1]
b = []
print(a)
c = []
for i in range(len(a)):
    for j in range(len(b)):
        if(a[i] == b[j]):
            count = count + 1

    if(count == 0):
        b.append(a[i])
    count = 0
print(b)

output:

[1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 1, 12, 4, 6, 2, 3, 7, 9, 4, 1]

[1, 2, 3, 4, 5, 6, 12, 7, 9]

Upvotes: 0

Ketouem
Ketouem

Reputation: 3857

A good old list comprehension with a filter inside should do the trick:

>>> a = [1, 1, 2, 3, 5, 3]
>>> b = [i for i in a if len(filter(lambda x,val=i: x == val, a)) == 1]
>>> b
[2, 5]

Be careful as it does not mutate the initial list but construct a new list.

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304147

This sort of matches the method you describe using set/bag

>>> from collections import Counter
>>> list_of_friends = ['bill','bill','mark','jenna','brad','mark']
>>> S = set(list_of_friends)
>>> bag = Counter(list_of_friends)
>>> S.difference(bag - Counter(S))
set(['brad', 'jenna'])

Upvotes: 2

mhawke
mhawke

Reputation: 87074

Just to add to the potpourri of answers, here's one using itertools.groupby(). This is kind of the same as the Counter m\ethod, but probably less efficient.

>>> import itertools
>>> list_of_friends = ['bill','bill','mark','jenna','brad','mark']
>>> [k for k,v in itertools.groupby(sorted(list_of_friends)) if len(list(v)) == 1]
['brad', 'jenna']

Upvotes: 0

Chris Arena
Chris Arena

Reputation: 1630

This is hideous and far too clever but it works:

list_of_friends = ['bill', 'bill', 'mark', 'jenna', 'brad', 'mark']
set_of_friends = set(list_of_friends)
[list_of_friends.remove(friend) for friend in set_of_friends]

real_friends = [friend for friend in set_of_friends if friend not in list_of_friends]
print(real_friends) # ['brad', 'jenna']

Upvotes: 0

ssm
ssm

Reputation: 5373

Or you can just use recursion:

In [83]: def retUn(x):
   ....:     if not x: return []
   ....:     if x[0] in x[1:]: return retUn( filter(lambda m: m != x[0], x) )
   ....:     else: return [ x[0] ] + retUn( x[1:] )
   ....:

In [84]: retUn(x)
Out[84]: ['jenna', 'brad']

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54193

if for some reason you can't use a Counter (ugh why?)

list_of_friends = ['bill','bill','mark','jenna','brad','mark']
a_set = set()
b_set = set()
for friend in list_of_friends:
    if friend not in a_set:
        a_set.add(friend)
    else:
        b_set.add(friend)
result = a_set - b_set

Upvotes: 1

lifeng.luck
lifeng.luck

Reputation: 601

Try this:

list_of_friends = ['bill','bill','mark','jenna','brad','mark']
unique_list_of_friends = [i for i in list_of_friends if list_of_friends.count(i) == 1]
print(unique_list_of_friends)

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304147

>>> from collections import Counter
>>> list_of_friends = ['bill','bill','mark','jenna','brad','mark']
>>> [k for k, v in Counter(list_of_friends).items() if v == 1]
['brad', 'jenna']
>>> 

Upvotes: 6

g.d.d.c
g.d.d.c

Reputation: 47988

You could use a Counter:

>>> from collections import Counter
>>> c = Counter(['bill','bill','mark','jenna','brad','mark'])
>>> [k for k in c.keys() if c[k] == 1]
['brad', 'jenna']

Upvotes: 3

Related Questions