Here is an implementation that makes use of the duck typing principle:
In [5]: def clear(seq):
...: for i, item in enumerate(seq):
...: try:
...: clear(item)
...: except TypeError:
...: try:
...: seq[i] = 0
...: except IndexError, KeyError:
...: pass
...:
In [6]: s = [1,3,4,[5,2,[4,3],3],6,[7,8],9]
In [9]: clear(s)
In [10]: s
Out[10]: [0, 0, 0, [0, 0, [0, 0], 0], 0, [0, 0], 0]
The advantage to this is that you aren't restricted to structures/values that are int
s or list
s. This function will 'zero' out any mutable structure which supports indexing.