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

Comparing two lists and printing the list in Python

I have two lists Z1 and Z2. I am comparing these two lists and if at least one sublist of Z1 appears in Z2, I want to print full Z1. I present the current and expected output.

Z1=[[0, 4], [0, 5], [4, 5]]
Z2=[[6, 5], [4, 9], [4, 8], [5, 3], [0,4]]

for i in range(0,len(Z1)):
    if (Z1[i]==Z2[i]):
        print("Z1 =",Z1)

The current output is

(Blank)

The expected output is

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

Z1=[[0, 4], [0, 5], [4, 5]]

>Solution :

An rather elegant solution can be using sets and their intersection:

Z1=[[0, 4], [0, 5], [4, 5]]
Z2=[[6, 5], [4, 9], [4, 8], [5, 3], [0,4]]

zs1 = set(map(tuple, Z1))
zs2 = set(map(tuple, Z2))

if (zs1 & zs2):
    print("Z1 =",Z1)
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