[Python-Dev] Proposal: dict.with_values(iterable)

Serhiy Storchaka storchaka at gmail.com
Fri Apr 12 11:35:00 EDT 2019


12.04.19 16:44, Inada Naoki пише:
> When creating many dicts with same keys, dict need to
> lookup internal hash table while inserting each keys.
> 
> It is costful operation.  If we can reuse existing keys of dict,
> we can skip this inserting cost.
> 
> Additionally, we have "Key-Sharing Dictionary (PEP 412)".
> When all keys are string, many dict can share one key.
> It reduces memory consumption.

It looks contrary to simplification made in Python 3 when we get rid of 
some more efficient lists in favor of more general iterators.

If this is a common case we can add an invisible optimization for 
dict(zip(keys, values)), especially if keys is a key-sharing dictionary. 
This will benefit all users without the need to rewrite the code to use 
the new special method.

The interface of dict is already overloaded. It contains many methods 
which most users use rarely (and therefore which are not kept in the 
working set of memory).



More information about the Python-Dev mailing list