Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grab the pair of different items from two lists in Python

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 1 != L21 –False (I don’t want it).
However, my code is comparing each item from L1 with each item from L2 and returning undesired pairs.

l1 =  ["pear", "papaya", "guava"]
l2=   ["grape", "pear", "guava"]

The code
enter image description here

I just would like to have :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pear: grape
papaya: pear

>Solution :

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]}') 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading