Python: Find list overlap with duplicates in lists
Question:
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']
Answers:
If you use a set
you can use the &
operator, which for sets does an intersection. This will eliminate duplicates and find common elements.
Edit: For your problem you will actually need a multiset. I recommend using the multiset
package which you can install with pip install multiset
or pip3 install multiset
if you’re using python3.
from multiset import Multiset
list1 = ['Z','Z','Z','Z','E','E']
list2= ['Z', 'Z', 'Z']
print(Multiset(list1) & Multiset(list2)) # Output: {'Z', 'Z', 'Z'}
As @It_is_Chris said in the comment you can use sets to remove duplicates. Sets also have a method to find common items:
list1 = ['Q, 'U', 'Z', 'Z']
list2 = ['Q, 'U', 'I', 'Z']
common = set(list1).intersection(set(list2))
The output is now a set, but can be converted to a list if needed.
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']
If you use a set
you can use the &
operator, which for sets does an intersection. This will eliminate duplicates and find common elements.
Edit: For your problem you will actually need a multiset. I recommend using the multiset
package which you can install with pip install multiset
or pip3 install multiset
if you’re using python3.
from multiset import Multiset
list1 = ['Z','Z','Z','Z','E','E']
list2= ['Z', 'Z', 'Z']
print(Multiset(list1) & Multiset(list2)) # Output: {'Z', 'Z', 'Z'}
As @It_is_Chris said in the comment you can use sets to remove duplicates. Sets also have a method to find common items:
list1 = ['Q, 'U', 'Z', 'Z']
list2 = ['Q, 'U', 'I', 'Z']
common = set(list1).intersection(set(list2))
The output is now a set, but can be converted to a list if needed.