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

Get on None value doesn't return default value of get

When I do a get on a dict that has the value None, it returns a None rather than the default value of the get

d = {"a": None}

d.get("a", {}).get("truc")

Is there any way to do this in one line?

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 :

None is still a value and the key exists, so yeah, you won’t get the default value. If you need a truthy value, do this:

(d.get('a') or {}).get('truc')
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