<div dir="ltr">Calling upon ol' Guidos Time Machinne:<br><br><br>In [31]: def create(val1):  <br>   ...:     data = {  <br>   ...:     **({"val1": "me here"} if val1 else {})  <br>   ...:     }  <br>   ...:     return data  <br>   ...:                                                                                                                    <br><br>In [32]: create(False)                                                                                                      <br>Out[32]: {}<br><br>In [33]: create(True)                                                                                                       <br>Out[33]: {'val1': 'me here'}<br><br>Now, please, just move to the next request to the language.<div><br>I think the in-place star exapanding, along with the inline if like above can make out</div><div>for all of your use-cases. If it can't, then expect xpression assignment-s (the `a:=1`  from PEP 572,</div><div>comming in Python 3.8) to cover for the rest - as you can define variables inside the `if` expressions</div><div>like the above which could be re-used in other `if`s (or even in key-names), further down the dictionary)</div><div><br></div><div><br><br>So, seriously - On one hand, Python's syntaxalready allow what you are requesting.<br>On the other hand, it makes use of a syntax that is already little used (in place star-expansion for mappings),</div><div>to a point that in this thread, up to here, no one made use of it.<br>Threfore, IMHO, it demonstrates  that adding arbitrary syntaxes and language features ultimatelly fills the language</div><div>with clutter very few people ends up knowing how to use - - we should now work on what </div><div>is already possible instead of making the language still more difficult to learn .<br><br></div><div>(respasting the code so you can further experiment):<br><span style="font-family:monospace"><span style="color:rgb(0,0,0)"><br></span></span></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">def create(val1):
</span><br>    data = {
<br>    **({"val1": "me here"} if val1 else {})
<br>    }
<br>    return data<br><br><br>
<br></span><span style="font-family:monospace"><font color="#008700"><br></font></span></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 26 Apr 2019 at 21:22, Christopher Barker <<a href="mailto:pythonchb@gmail.com">pythonchb@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><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>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>