Python - List Indexing, Slicing, and Matrixes


Lists are sequences, indexing and slicing work the same way for lists as they do for strings.

The result of indexing a list is whatever type of object lives at the offset you specify.

Slicing a list always returns a new list:


L = ['test', 'Test', 'TEST!'] 
print( L[2] )                              # Offsets start at zero 
print( L[-2] )                             # Negative: count from the right 
print( L[1:] )                             # Slicing fetches sections
# from   w w w . j a v  a 2 s  . c o m