You should consider using a dict
type instead of pre-initialized list. The cost of a dictionary look-up is small and comparable to the cost of accessing arbitrary list element.
And when using a mapping you can write:
aDict = {}
aDict[100] = fetchElement()
putElement(fetchElement(), fetchPosition(), aDict)
And the putElement
function can store item at any given position. And if you need to check if your collection contains element at given index it is more Pythonic to write:
if anIndex in aDict:
print "cool!"
Than:
if not myList[anIndex] is None:
print "cool!"
Since the latter assumes that no real element in your collection can be None
. And if that happens - your code misbehaves.
And if you desperately need performance and that's why you try to pre-initialize your variables, and write the fastest code possible - change your language. The fastest code can't be written in Python. You should try C instead and implement wrappers to call your pre-initialized and pre-compiled code from Python.