I thought of something similar, but where the dict-literal construction is 
desired:

>>> foo = True
>>> bar = False
>>> baz = False
>>> d = {
...   'foo' if foo else None: 1,
...   'bar' if bar else None: 2,
...   'baz' if baz else None: 3,
... }
>>> d
{'foo': 1, None: 3}
>>> d.pop(None)
3
>>> d
{'foo': 1}

The natural helper function for the "'foo' if foo else None" snip would then be 
something like:

>>> def key_flag(key, flag, default=None):
...     return key if flag else default
...
>>> dd = {
...   key_flag('foo', foo): 1,
...   key_flag('bar', bar): 2,
...   key_flag('baz', baz): 3,
... }
>>> dd
{'foo': 1, None: 3}
>>> dd.pop(None)
3
>>> dd
{'foo': 1}

-Brian
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/SZZVOFRPJEWTK5BJLEZVVSAZAL5F3OT7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to