I am new to Python. I have a number of objects that need to be inserted at certain indexes on the list, but they fail, so I can’t just add them. How can I grow a list when necessary to avoid IndexErrors?
def set(index, item):
if len(nodes) <= index:
nodes[index] = item
I know that you can create a list with an initial throughput through nodes = (index+1) * [None], but what is the usual way to grow it? The following functions do not seem to be effective:
for _ in xrange(len(nodes), index+1):
nodes.append(None)
Also, I suppose there is a class in the standard library that I should use instead of inline lists?
source
share