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

Michael Selik mike at selik.org
Wed Feb 17 18:22:32 EST 2016


> On Feb 16, 2016, at 6:48 PM, Ian Foote <ian at feete.org> wrote:
> 
> I think this is one of those cases where the gain is small enough that
> writing the helper function is often not worth it, but big enough that
> people would use it if it already existed. That might be a small window,
> but it's there.

I’m pretty sure I would stick with ``dict(zip(keys, values))``. My code would then be backwards-compatible and wouldn’t introduce yet another thing for a newbie to need to learn about when reading my code.

A new alternate constructor should follow the pattern ``dict.from_stuff`` or ``dict.fromstuff`` like ``dict.fromkeys`` or ``datetime.fromordinal``. What would you call this one? The method ``dict.setdefault`` is already awkwardly named. I suppose ``dict.get_value_and_insert_key_if_missing`` is prohibitively long. Would this new one be ``dict.from_keys_and_values``?

Actually, that makes me think of one version I might be OK with:

    dict.fromkeys('abc', values=[0, 1, 2])

But then what would be the benefit versus ``dict(zip(keys, values))``?


More information about the Python-ideas mailing list