Also, if one have in mind that dict addition with the `+` operator had been
recelently approved, as of Python 3.9 it will be ok to write:

```
 return ({
        'user_id': user_id,} +
        ({'max_results': max_results} if max_results else {}) +
        ({'active': active} if active is not None else {}) +
        ({'deleted': deleted} if deleted is not None else {})
    )

And actually, on a second though, this is already valid as of current
Python  - and is potentially more
readable than a magic "dict.skip" singleton.
```
 return {
     'user_id': user_id,
     **({'max_results': max_results} if max_results else {}),
     **({'active': active} if active is not None else {}),
     **({'deleted': deleted} if deleted is not None else {})
 }
```

On Thu, 5 Sep 2019 at 10:22, <brian.sk...@gmail.com> wrote:

> 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 -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/SZZVOFRPJEWTK5BJLEZVVSAZAL5F3OT7/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2MRYBEHOWRE6YCGNHKXCPAVUITVPNETF/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to