[Python-ideas] Dict(x, y) -> Dict(zip(x, y))

Andrew Barnert abarnert at yahoo.com
Tue Feb 16 20:05:13 EST 2016


On Feb 16, 2016, at 16:34, MRAB <python at mrabarnett.plus.com> wrote:
> 
>> On 2016-02-16 23:48, Ian Foote wrote:
>>> On 15/02/16 09:53, Andrew Barnert via Python-ideas wrote:
>>> 
>>> 
>>> 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)
> Going the other way it's "my_dict.items()", so perhaps "dict.from_items(spam, eggs)"?

But items() is the inverse of the plain constructor, which takes an iterable of items. In other words, both items() and the constructor deal in a single iterable of zipped key-value pairs.

The proposed new method or function instead deals in unzipped, separate key and value iterables. So it's the inverse of "my_dict.keys(), my_dict.values()".

Of course you can also write that as "zip(*my_dict.items())", just as you can also write the new method as "dict(zip(keys, values))".

At any rate, since you can write any variation of this as a one-liner, as long as you're willing to spell it "dict_spam" instead of "dict.spam", each person can call it whatever they want. :)


More information about the Python-ideas mailing list