Reputation: 797
I have following python code:
a = [0,1,2,3,4,5]
del(a[2:7])
Should not this give "IndexError"? If no then why?
Upvotes: 0
Views: 64
Reputation: 89027
This deletes the list slice from 2 to before 7. List slices do not throw index errors, rather if they extend beyond the end of the list, they return the whole remainder of the list.
>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a [5:20]
[5, 6, 7, 8, 9]
Upvotes: 2