[Python-ideas] Dict(x, y) -> Dict(zip(x, y))
Andrew Barnert
abarnert at yahoo.com
Mon Feb 15 04:53:20 EST 2016
On Feb 14, 2016, at 18:43, Bruce Leban <bruce at leban.us> wrote:
>
> this would have:
>
> >>> dict(((1,2),(3,4)))
> {1: 2, 3: 4}
>
> >>> dict(((1,2),(3,4)), ((5,6)))
> {(1, 2): 5, (3, 4): 6}
>
> in other words, the interpretation of the first argument to dict is entirely different if there is a second argument than if there isn't one.
It would be nice if we could make Oscar's new parameters keyword-only:
dict(keys=spam, values=eggs)
Then it's _not_ the first argument that's being interpreted differently.
Unfortunately, we can't do this, because it would be ambiguous with the dict(a=1, b=2) constructor.
But what about a classmethod named constructor (with or without keyword-only params, depending on how long the name is):
dict.kv(keys=spam, values=eggs)
dict.from_keys_values(spam, eggs)
That's pretty close to the idiomatic way to build dictionaries in ObjectiveC:
[NSDict dictionaryWithValues:eggs forKeys:spam]
... so it's not totally unprecedented.
Still, it raises the obvious question of whether "dict.kv" is much better than "dictkv", which anyone can write for himself as a one-liner if he wants it...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160215/7a42302a/attachment-0001.html>
More information about the Python-ideas
mailing list