I need a list with the following behavior
>>> l = SparseList()
>>> l
[]
>>> l[2] = "hello"
>>> l
[ None, None, "hello"]
>>> l[5]
None
>>> l[4] = 22
>>> l
[ None, None, "hello", None, 22]
>>> len(l)
5
>>> for i in l: print i
None
None
"hello"
None
22
Although it can “emulated” via a dictionary, it’s not exactly the same. numpy array can behave this way, but I don’t want to import the whole numpy for something like this. Before coding it myself, I ask if something similar exists in the standard library.
Here’s minimal code to pass your given examples (with indispensable adjustments: you expect weird spacing and quoting, ‘None’ to be printed out at the prompt without a
printstatement, etc etc):I imagine you’ll want more (to support negative indices, slicing, and whatever else), but this is all your examples are implicitly specifying.