Hi, what is a more pythonic way to set a value in python to any index in a list?

Viewed 109

I am creating my own data structure in python which I call an Array. One property that I would like it to have is if for instance array = [1,2], one could write array[5] = 6 and then array = [1,2,None,None,None,6]. I have accomplished this, but my code seems very awkward.

def __setitem__(self,index,value):
    try:
        self.array[index] = value
    except IndexError:
        if index+1 > len(self):
            add = index + 1 - len(self)
            self.array += [None] * add
            self.array[i] = value
2 Answers
Related