I have the following lists:
list_1_test = [['hi','there','how'],['we','are','one']]
list_2_test = [['hi','you','how'],['we','not','one']]
I wish to compare the words in the list and get the following output:
list_3_test = [['there','you'],['are','not']]
I know how to do this in a simple list, for example:
list_1_test = ['hi','there','how']
list_2_test = ['hi','you','how']
list_3_test=[]
for i in list_1_test:
if i not in list_2_test:
list_3_test.append(i)
for i in list_2_test:
if i not in list_1_test:
list_3_test.append(i)
and the result is
['there', 'you']
But when it comes to list of lists, my brain is fried. The order matters. Any help is much appreciated.
CodePudding user response:
If order doesn't matter, you can use set operations list comprehension:
out = [list(set(l1).union(l2) - set(l1).intersection(l2)) for l1, l2 in zip(list_1_test, list_2_test)]
Output:
[['you', 'there'], ['are', 'not']]
If order matters, you can use dict.fromkeys
:
out = []
for l1, l2 in zip(list_1_test, list_2_test):
one = dict.fromkeys(l1).keys()
two = dict.fromkeys(l2).keys()
out.append(list(one - two) list(two - one))
Output:
[['there', 'you'], ['are', 'not']]
CodePudding user response:
You can use the sets and symetric_difference method (^):
list3 = []
for l1, l2 in zip(list_1_test, list_2_test):
sym_dif = set(l1)^set(l2)
list3.append(list(sym_dif))
Previous code in list comprehension format:
list3 = [set(l1)^set(l2) for l1, l2 in zip(list_1_test, list_2_test)]