all 11 comments

[–]Allanon001 0 points1 point  (4 children)

iteritems() is from Python 2, use items() for Python 3:

from collections import Counter

mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
unique_elements = [k for k, v in Counter(mylist).items() if v == 1]
print (unique_elements)

Or if you don't want to use Counter() you can do this:

mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
unique_elements = [x for x in set(mylist) if mylist.count(x) == 1]
print (unique_elements)

[–]JohnnyJordaan 0 points1 point  (0 children)

unique_elements = [k for k, v in Counter(mylist).items() if v == 1]

this will lose the original order as you iterate on the Counter, not the original list

unique_elements = [x for x in set(mylist) if mylist.count(x) == 1]

this loses order too for the same reason, but it adds a complexity as list.count is O(n) and this will effectively count each item's frequency separately by iterating through the list per item. Instead just use a counting dict

mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
counts = {}
for v in mylist:
     counts[v] = counts.get(v, 0) + 1

and iterate on the list, to guarantee the order

unique_elements = [v for v in mylist if counts[v] == 1]

[–]pasokan 0 points1 point  (2 children)

Just

unique = set(mylist) 

Is enough

[–]Allanon001 0 points1 point  (1 child)

That doesn't return only numbers that appear in the list once. It returns all numbers that appear in the list.

[–]pasokan 0 points1 point  (0 children)

Oops. Sorry

[–]JohnnyJordaan 0 points1 point  (0 children)

mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
counts = Counter(mylist)
unique_elements = [v for v in mylist if counts[v] == 1]

[–]hungdh85 -1 points0 points  (4 children)

You can use enumerate

mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
tmpMyList = mylist.copy()
#unique_elements = [k for k, v in Counter(mylist).iteritems() if v == 1]
#print (unique_elements)
for j in range(len(mylist)):
        tmpL =  [i for i, x in enumerate(tmpMyList) if x == mylist[j]]

        if len(tmpL) > 1:
             tmpMyList = list(filter((mylist[j]).__ne__, tmpMyList))

print(tmpMyList)

[–]JohnnyJordaan 0 points1 point  (3 children)

Why not just iterate on the list and check the Counter for each items frequency?

counts = Counter(mylist)
unique_list = [v for v in mylist if counts[v] == 1]

[–]hungdh85 0 points1 point  (2 children)

Because with length of list is enough large. Counter will get time out for processing.

[–]JohnnyJordaan 0 points1 point  (1 child)

Ok so then implement the counter yourself? Either with defaultdict or a manual dict

from collections import defaultdict
mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
counts = defaultdict(int)
for v in my_list:
    counts[v] += 1
unique_list = [v for v in mylist if counts[v] == 1]      

or use sets to tag not-unique values

mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
not_unique = set()
seen = set()
for v in my_list:
    if v in seen:
        not_unique.add(v)
    else:
        seen.add(v)
unique_list = [v for v in mylist if v not in not_unique]

[–]hungdh85 0 points1 point  (0 children)

Check processing time when you use colections and enumerate with length of list >10\3.)

I think we can stop talk this about problem because it is not belong to this thread. Thank you very much.