Most efficient way to "pre-grow" a list?

r rt8396 at
Sat Nov 7 04:06:29 CET 2009

On Nov 6, 6:12 am, kj <no.em... at> wrote:
> In Perl one can assign a value to any element of an array, even to
> ones corresponding to indices greater or equal than the length of
> the array:
>   my @arr;
>   $arr[999] = 42;
> perl grows the array as needed to accommodate this assignment.  In
> fact one common optimization in Perl is to "pre-grow" the array to
> its final size, rather than having perl grow it piecemeal as required
> by assignments like the one above:
>   my @arr;
>   $#arr = 999_999;
> After assigning to $#arr (the last index of @arr) as shown above,
> @arr has length 1,000,000, and all its elements are initialized to
> undef.
> In Python the most literal translation of the first code snippet
> above triggers an IndexError exception:
> >>> arr = list()
> >>> arr[999] = 42
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> IndexError: list assignment index out of range
> In fact, one would need to pre-grow the list sufficiently to be
> able to make an assignment like this one.  I.e. one needs the
> equivalent of the second Perl snippet above.
> The best I can come up with is this:
> arr = [None] * 1000000
> Is this the most efficient way to achieve this result?
> TIA!
> kynn

You mean sum'in like dis?

class PerlishList(list):
    '''Hand holding list object for even the most demanding Perl
    def __init__(self, dim=0):
        if dim:
            self.__setitem__(dim, None)

    def __setitem__(self, idx, v):
        lenkeys = len(self)
        sup = super(PerlishList, self)
        if idx > lenkeys:
            for idx in range(lenkeys, idx):
        sup.__setitem__(idx, v)

    def __getitem__(self, idx):
        return self[idx]

l = PerlishList(3)
print l
l[10] = 10
print l


More information about the Python-list mailing list