[issue17338] Add length_hint parameter to list, dict, set constructors to allow efficient presizing

Raymond Hettinger report at bugs.python.org
Sat Mar 2 22:48:38 CET 2013


Raymond Hettinger added the comment:

Guido has previously rejected any kind of user specified presizing parameter for dictionaries.  He felt that it was breaking the abstraction in a way that caused people to focus on implementation specific details.

Also, set() and dict() use presizing (and hash value reuse) only in the limited case of the input iterable being an exact dictionary or set.  Otherwise, it makes no assumptions about the input iterable and grows as needed.  For sets, even when the __len__ is known for a general iterable, we don't know how many unique elements are the input -- consider for example: set([None] * 1000000).  In typical use cases for sets, a user also doesn't know in advance how many unique elements will be present -- and to the extent they try to add estimation logic, it would only complicate their programs.

----------
nosy: +rhettinger

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17338>
_______________________________________


More information about the Python-bugs-list mailing list