Mimic JavaScript arrays in Python -


in python, possible mimic javascript arrays (i. e., arrays expand automatically when values added outside array's range)? in javascript, arrays expand automatically when value assigned outside of array's index, in python, not:

thearray = [none] * 5 thearray[0] = 0 print(thearray) thearray[6] = 0 '''this line invalid. python arrays don't expand automatically, unlike javascript arrays.''' 

this valid in javascript, , i'm trying mimic in python:

var thearray = new array(); thearray[0] = 0; console.log(thearray); thearray[6] = 0; //the array expands automatically in javascript, not in python 

if need can define such structure:

class expandinglist(list):     def __setitem__(self, key, value):         try:             list.__setitem__(self, key, value)         except indexerror:             self.extend((key - len(self)) * [none] + [value])  >>> = expandinglist() >>> a[1] = 4 >>> [none, 4] >>> a[4] = 4 >>> [none, 4, none, none, 4] 

integrating other python features might tricky (negative indexing, slicing).


Comments