preallocate list

Jim jbo at cannedham.ee.ed.ac.uk
Wed Apr 13 13:34:19 EDT 2005


> ivec = n*[None]
> 
> so that if I use a list element before intializing it, for example
> 
> ivec[0] += 1
> 
> I get an error message
> 
>   File "xxnone.py", line 2, in ?
>     ivec[0] += 1
> TypeError: unsupported operand type(s) for +=: 'NoneType' and 'int'
> 
> This is in the same spirit as Python's (welcome) termination of a
> program when one tries to use an uninitalized scalar variable.
> 

I feel foolish that I forgot about *. I've just started with Python then 
took 2 weeks off. I'll explore pre-allocation when I'm back up to speed.

Yep, I use None a lot.

Thanks

Jim



More information about the Python-list mailing list