Python - List Bounds Checking

Introduction

Although lists have no fixed size, Python still doesn't allow us to reference items that are not present.

Indexing off the end of a list is always a mistake, but so is assigning off the end:

L = [123, 'test', 'book2s.com'] 

>>> L[99] 
 ...error text omitted... 
IndexError: list index out of range 

>>> L[99] = 1 
 ...error text omitted... 
IndexError: list assignment index out of range 

Related Topic