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

literal_eval and boolean Logic in Python

>>> from ast import literal_eval
>>> H = {"('a','b')":1}
>>> x = ('a','b')
>>> str(x)
"('a', 'b')"
>>> list(H.keys())[0]
"('a','b')"
>>> str(x) == list(H.keys())[0]
False

Why do I get a False statement? However, when I do

>>> x == literal_eval(list(H.keys())[0])
True

I get a True statement.

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 :

In my tests, str(x) is "('a', 'b')". Do you notice the space after the comma?

That is enough to explain why the strings are different (one contains a space while the other does not), while the tuples are equal.

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