I am trying to create a file with only the pairs of items that differ in two lists. I would like it to be position limited: L1 [0] != L2[0] --True (I want it!). L1
I just would like to have :
pear: grape
papaya: pear
CodePudding user response:
If you can assume the length.. then you can simply take the index and compare equal indexes.
l1 = ["pear", "papaya", "guava"]
l2= ["grape", "pear", "guava"]
for index in range(len(l1)):
if (l1[index] != l2[index]):
print(f'{l1[index]}: {l2[index]}')
CodePudding user response:
Assuming the you lists have same length you can use the build-in function zip
which return a generator of pairs.
l1 = ["pear", "papaya", "guava"]
l2= ["grape", "pear", "guava"]
print([f'{i}: {j}' for i, j in zip(l1, l2) if i != j])
If the length are different a combinatorics approach is suggested.