Often when I write some code I would have a rough idea of how long the list will be. I guess if I try really hard I can calculate the proper size of it at the beginning, but that's just a lot of things I need to take care of (like making sure there is no extra None at the end that messes things up or making sure I don't index out of the range later).
It seems like I would have to choose from pre-allocate some memory and index into it. Or just create an empty space and use the list.append() method to populate my list. Like either this:
A = [None]*1000
for i in range(1000):
A[i] = 1
or this:
B = []
for i in range(1000):
B.append(1)
My question is are there some intermediate methods? Can I do something like pre-allocate around 1000 units of memory to a list, but when I append it add after the last cell with the actual item instead of after the cell I set to be None?
The idea is something like this (but this doesn't actually work):
A = [None]*1000
for i in range(1000):
A.append(1)
Is this possible with the Python list? Or do I have to use some other data structure? Or it's just too random a thought?
I don't really understand why you'd want to do this. You're unlikely to achieve any gains in terms of performance by pre-allocating a 1000 elements, but you can create a user defined class along the lines of:
And then you can use it like this:
The end result is likely to be much slower since the base list uses C under the hood and any changes to the normal behaviour will actually reduce the performance: