I am asked to compare 2 list and filer out unique value in a third list. I must ensure that the third list only contains unique values, no doubles.
Following code works :
import os, random
def makerange(number):
lijst = [random.randint(1,number) for item in
range(1,random.randint(2,number))]
return lijst
a = makerange(20)
b = makerange(20)
c = set()
for item in a:
if item in b and item not in c:
c.add(item)
I've tried to rewrite the for loop to a python list comprehension.
c = [ item for item in a if (item in b) & (item not in c)]
However this list comprehension does not work ? Any suggestions why this is not working ? And how should i write this with a list comprehension.