17

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.

3 Answers 3

26

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 print statement, etc etc):

class SparseList(list):
  def __setitem__(self, index, value):
    missing = index - len(self) + 1
    if missing > 0:
      self.extend([None] * missing)
    list.__setitem__(self, index, value)
  def __getitem__(self, index):
    try: return list.__getitem__(self, index)
    except IndexError: return None

__test__ = dict(allem='''
>>> l = SparseList()
>>> l
[]
>>> l[2] = "hello"
>>> l
[None, None, 'hello']
>>> print 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
''')
import doctest
doctest.testmod(verbose=1)

I imagine you'll want more (to support negative indices, slicing, and whatever else), but this is all your examples are implicitly specifying.

Sign up to request clarification or add additional context in comments.

4 Comments

However, this means that it does not exist in the standard library... may I include your code in the wavemol (BSD) library ?
@Stefano, sure, see meta.stackexchange.com/questions/13976/… : "user contributed content licensed under cc-wiki with attribution required" per Jeff Attwood's answer (I believe the BSD license is compatible w/that, but I'll be happy to relicense it to you otherwise as needed!-).
Do Python lists automatically optimize space for this?
A little testing shows they don't, so while this meets the OP's stated requirements, it doesn't meet the usual expectations of a sparse array.
7

Dictionaries can be used as sparse lists. Whilst they will not provide the characteristics you are after (as you are not actually after a sparse list, all the list elements are complete references to None in a dynamically-sized Array), they act as a textbook sparse array.

sparse_vars = [(0,"Hi"), (10000,"Bye"), (20000,"Try")]
sparse_list = {}

for var in sparse_vars:
  sparse_list[var[0]] = var[1]

>>> print sparse_list
{0: 'Hi', 10000: 'Bye', 20000: 'Try'}
>>> print sparse_list[20000]
'Try'

1 Comment

If you need to iterate over the sparse array items and the order is significant, a dictionary-based solution would be inefficient
0

The sparse_list package provides the behaviour OP asks for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.