[Python-Dev] Proposal: dict.with_values(iterable)
vstinner at redhat.com
Fri Apr 12 10:31:47 EDT 2019
Nice optimization! I have questions on the proposed API.
> with_values(self, iterable, /)
> Create a new dictionary with keys from this dict and values from iterable.
> When length of iterable is different from len(self), ValueError is raised.
> This method does not support dict subclass.
In short, mydict.with_values(values) behaves as
dict(zip(mydict.keys(), values)), but is more efficient?
The method rely on the fact that dict is preserving key insertion order, right?
Le ven. 12 avr. 2019 à 15:47, Inada Naoki <songofacandy at gmail.com> a écrit :
> This might be usable for:
> * csv.DictReader
> * namedtuple._asdict()
> * DB-API 2.0 implementations: (e.g. DictCursor of mysqlclient-python)
I guess that a new dict constructor taken keys and values like
dict.from_keys_and_values(keys, values) would work, but would not
benefit from the dict key-sharing optimization?
Would it be possible to implement the key-sharing optimization using a
dict.from_keys_and_values(mydict.keys(), values) method: detect that
keys are owned by a dict, and so create a new dict linked to the keys
dict? A dict view contains a reference to the iterated dict
I'm fine with dict.with_values() API, but I'm asking if it could be
Night gathers, and now my watch begins. It shall not end until my death.
More information about the Python-Dev