[Python-Dev] Dictionary Foolishness? (original) (raw)

Raymond Hettinger Raymond Hettinger" <python@rcn.com
Wed, 20 Nov 2002 15:12:56 -0500


Have dictionaries support the repetition. Use the replication factor to provide a hint as to how large the dictionary is expected to be.

[0] * n # allocate an n-length list {} * n # allocate an n-element dictionary

If the a list already has k elements, the result is already nk long; however, for a dictionary, since the keys are unique, the result says at n1. Hence, the dictionary contents are unaffected by repetition. But the n-factor is useful for eliminating all but one of the re-size operations when growing a large dictionary, element by element.

If k > n, then the hint is disregarded since the number of elements already exceeds the hint.

Raymond Hettinger