I’m given a problem where I have to filter out dupes from a list such a
a = [1,1,4,5,6,5]
This is my code:
def unique(a):
uni = []
for value in a:
if value[0] not in found:
yield value
found.add(value[0])
print list(unique(a))
However, when I define the list, a, and try unique(a), I get this output:
<generator object unique at 0x0000000002891750>
Can someone tell me what I’m doing wrong? Why can’t I get the list?
EDIT, NEW PROBLEM..
I was able to get it print out the filtered list, but I lose the order of the list.
How can I prevent this?
def unique(a):
s = set()
for i in a:
if i not in s:
s.add(i)
return s
You have to keep track of all the elements that have been seen. The best way is to use
setas lookup complexity of it isO(1).If you don’t need to keep the order of the elements you can utilize the
setconstructor, and then convert it back to list. This will remove all the duplicates, but will destroy the order of the elements: