Python slicing weirdity
October 17th, 2004
Huh?
>>> a=range(10) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a[None] = 20 Traceback (most recent call last): File “<stdin>”, line 1, in ? TypeError: list indices must be integers >>> a[None:None] = [20] >>> a [20]
Yes, I actually ran into this, because I mistakenly inserted a value
into a list by slicing it with the result of insort_right()
(which returns None) when I clearly meant to call
bisect_right() (which returns an index into the list).