Compare two lists and find duplicate that exist in both lists from two lists remove the existing duplicates and create new lists.
Input
a= [
"DJI_0229.jpg",
"DJI_0232.jpg",
"DJI_0233.jpg"
"DJI_0235.jpg"
]
b= [
"DJI_0229.jpg",
"DJI_0232.jpg",
"DJI_0233.jpg"
"DJI_0230.jpg",
"DJI_0231.jpg",
"DJI_0234.jpg"
]
output =
[
"DJI_0230.jpg",
"DJI_0231.jpg",
"DJI_0234.jpg",
"DJI_0235.jpg"
]
CodePudding user response:
you can do this
a= ["DJI_0229.jpg","DJI_0232.jpg","DJI_0233.jpg","DJI_0235.jpg"]
b= ["DJI_0229.jpg","DJI_0232.jpg","DJI_0233.jpg","DJI_0230.jpg","DJI_0231.jpg","DJI_0234.jpg"]
c = []
for image in (a b):
if image not in c:
c.append(image)
else:
c.remove(image)
print(c)
CodePudding user response:
you can use this
def remove_dupilicate(List1,List2):
return [item for item in List1 if item not in List2]
new_a = remove_dupilicate(a, b)
new_b = remove_dupilicate(b, a)
print(new_a,new_b)
output for new_a list is ['DJI_0235.jpg']
and new_b list is ['DJI_0230.jpg', 'DJI_0231.jpg', 'DJI_0234.jpg']
CodePudding user response:
The most simplest way to achieve what you want is using symmetric difference of sets. Consider the below venn diagram :
Code implementation
from pprint import pprint
a = ["DJI_0229.jpg", "DJI_0232.jpg", "DJI_0233.jpg", "DJI_0235.jpg"]
b = [
"DJI_0229.jpg",
"DJI_0232.jpg",
"DJI_0233.jpg",
"DJI_0230.jpg",
"DJI_0231.jpg",
"DJI_0234.jpg",
]
final_list = sorted(
list(
set(a).symmetric_difference(set(b)) # Using some maths :-)
) # Converting the set to list
) # Sorting the resultant list
pprint(final_list)