Get difference between two lists with Unique Entries
You can use the set data type to find the difference between two lists with unique entries. The set data type only stores unique values, so it will automatically remove any duplicates. Here's an example code snippet in Python:
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
# Find the difference between the two lists
diff = list(set(list1) - set(list2))
print(diff)
# Output: [1, 2]
Watch a video course
Python - The Practical Guide
This will return a new list containing the unique elements that are in list1
but not in list2
.
You can also use symmetric_difference() function of set which returns the unique elements of both list1 and list2
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
diff = list(set(list1).symmetric_difference(set(list2)))
Both these code snippet will give you the same output.