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

what is the difference between these 2 python codes, and why the out put is not same?

these are the 2 python3 code,

{w for w in words if w[::-1] in words and len(w)==4}
{w for w in words if w==w[::-1]and len(w)==4}

In my point, w is the word in words, so w should equal with words,but the output is differently. Could someone help me, why the output is different?

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

>Solution :

if words is {'abba', 'abcd', 'dcba'} then the result of the first code will be {'abba', 'abcd', 'dcba'} while the second will return {'abba'}.

w==w[::-1] checks if the word is equal to its reverse while w[::-1] in words checks if the reverse of w is in words.

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