<div dir="ltr"><div dir="ltr">Others have responded, but a note:<br><br>> What I want to do is:<br><div><br></div><div>```</div>def my_func(val_1, val_2):<br>    return {<br>        "field_1": val_1 if val_1,<br>        "next_depth": {<br>            "field_2": val_2 if val_2<br>        }<br>    }<br>```<br><div><br></div><div>I am finding this very confusing as to how to generalize this:</div><br>How do we know that val_1 belongs to the "top-level" field_1, and val_2 is in the nested dict with field_2?<br><br>Or:<br>```<br>def my_func(val_1, val_2):<br>    return {<br>        if val_1 : "field_1": val_1,<br>        "next_depth": {<br>            if val_2: "field_2": val_2<br>        }<br>    }<br><br>but this makes it seem like that distinction is hard-coded -- so is the nested dict is relevant?<br><br>> The more core syntax, which should be valid throughout the language, would be to have statements like `x = y if cond`<br><br>we have the</div><div dir="ltr"><br></div><div dir="ltr"><span style="font-family:monospace,monospace">x = y if cond else</span></div><div dir="ltr"><br></div><div dir="ltr">expression already -- and an assignment HAS to be assigned to something, so it seems what you want is:<br><br>x = y if cond else None<br><br>Maybe the "else None" feels like too much typing, but I prefer the explicitness myself. (and look in the history of this thread for "null coalescing" discussion, that _may_ be relevant.<br><br>The first of these intuitively reorganizes to `if cond: x = y`<br><br>then what do we get for x  `if not cond`? it ends up undefined? or set to whatever value it used to have?<br></div><div dir="ltr"><br></div><div dir="ltr">Frankly, I think that's a mistake -- you're going to end up with having to trap a NameError or do a a hasattr() check later on anyway. It's generally considered good practice to set a name to None if it isn't defined, rather than not defining it.<br><br>> and `x[y if cond]` ... But the second is not as clear, with a likely equivalent of `if cond: x[y] else raise Exception`.<br><br>assuming x is a dict, then you could do:<br><br><span style="font-family:monospace,monospace">d[y if cond else []] = value</span><br><br>It's a hack, but as lists aren't hashable, you get an TypeError, so maybe that would work for you?<br><br>example:<br><br>In [16]: key = "Fred"<br>In [17]: value = "Barnes"<br>In [18]: d = {}<br><br>In [19]: # If the key is Truthy:<br>In [20]: d[key if key else []] = value<br><br>In [21]: d<br>Out[21]: {'Fred': 'Barnes'}<br><br>In [22]: # if the key is Falsey:<br>In [23]: key = None<br><br>In [24]: d[key if key else []] = value<br>---------------------------------------------------------------------------<br>TypeError                                 Traceback (most recent call last)<br><ipython-input-24-170a67b9505a> in <module>()<br>----> 1 d[key if key else []] = value<br><br>TypeError: unhashable type: 'list'<br><br>-CHB<div><br></div><br><br><br>--<br>Christopher Barker, PhD<br><br>Python Language Consulting<br>  - Teaching<br>  - Scientific Software Development<br>  - Desktop GUI and Web Development<br>  - wxPython, numpy, scipy, Cython</div></div>