Given two list I need to make a third list which contains elements that occur only twice in over all list 1 and list 2. How to do it efficienlty with reasonable time and space complexity ?
my solution: using dictionary:
from collections import defaultdict
L=['a','b','c','d','a','d','e','e','g','h']
K=['a','g','i','g','g','i','r','r']
d=defaultdict(int)
for i in L:
d[i]+=1
for j in K:
d[j]+=1
print d
result=[]
for key,val in d.iteritems():
if val == 2:
result.append(key)
print result
My desired output would be:
['e', 'd', 'i', 'r']
Can I get a better pythonic solution?
Thanks.