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

Is there some syntax to unpack a tuple but have an alternate value if one of the values is falsy?

Example I might have:

x = None
y = x or 0

In a tuple, am I able to somehow unpack the tuple but give some default value if a falsy value is found? Example:

values = (1, None)
one or 0, two or 0 = values

I know that my solution doesn’t work but I was wondering if there is some one-line syntax that does.

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 :

You could use a generator expression:

one, two = (v or 0 for v in values)
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