So I have two list. For example:
list1 = [a1, b2, c3, f6]
list2 = [a1, b2, d4, e5]
And I want only values that uniquely appear within list2 to append to a new list. For example:
list3 = [d4, e5]
So I have two list. For example:
list1 = [a1, b2, c3, f6]
list2 = [a1, b2, d4, e5]
And I want only values that uniquely appear within list2 to append to a new list. For example:
list3 = [d4, e5]
You could use a list comprehension
.
list3 = [item for item in list2 if item not in list1]
Output
list3 = ['d4', 'e5']
You can convert list1
to a set first so you can efficiently test if an item in list2 is in the set while you iterate over list2
:
list1 = ['a1', 'b2', 'c3', 'f6']
list2 = ['a1', 'b2', 'd4', 'e5']
set1 = set(list1)
list3 = [i for i in list2 if i not in set1]
list3
would become:
['d4', 'e5']