Lists being compared
list1 = ['Q, 'U', 'Z', 'Z']
List2 = ['Q, 'U', 'I', 'Z']
My query
common = []
for letter in list1: # QUZZ
if letter in list2: # QUIZ --- this is the problematic condition (it is add 2 ZZ's instead of 1)
common.append(letter)
print(common)
Expected Output
Common = ['Q, 'U', 'Z']
Actual Output
Common = ['Q, 'U', 'Z', 'Z']
To add, the query should also work for the reverse condition. I have tried using sets, but that doesn't work for the second condition
list1 = ['Z','Z','Z','Z','E','E']
list2= ['Z', 'Z', 'Z']
print(list(set([x for x in list1 if x in list2])))
Expected Output: ''' ['Z','Z','Z']
'''
Actual Output:
['Z']