Because lists are sequences, indexing and slicing work the same way for lists as they do for strings (which are essentially a definition of ordered collections of characters, so accessible by position). The key to slicing and indexing is the offset. Python starts at 0 and ends at on less than the length of the string or list. This is important, because the latest character in a string or object in a list is not addressable. You can also use negative offsets, think of this as counting backward from the end. The picture below shows how offsets can be used.
Consider the following string and let's pull the first and last character of that string:
>>> S="flyffy bunny"
and slicing (extracting a section):
>>> S[-5:-1] #negative offset slicing, count from right
or just apply it straight:
>>> 'flyffy bunny'[1:3]
>>> 'flyffy bunny'[slice(1,30)]
Now consider the following list, containing 3 objects:
>>> L = ['spam', 'Spam', 'SPAM!']
>>> L #offset starts at 0, which is the object on the left
>>> L #Negative; count offset from the right
>>> L[1:] # Slicing fetches sections (1st offset from left)
Slices can be used to extract columns of data, and to prefix or remove leading and trailing text.
• S[1:3] fetches items at offsets 1 up to but not including 3.
• S[1:] fetches items at offset 1 through the end (the sequence length).
• S[:3] fetches items at offset 0 up to but not including 3.
• S[:−1] fetches items at offset 0 up to but not including the last item.
• S[:] fetches items at offsets 0 through the end—making a top-level copy of S.
As of python 2.3, splice expressions allow a third, optional index: the step/stride.
This extended slice, takes characters between offset 0 and 10 by steps of 2, so 1 3 5 7 and 9
You can also use a negative stride.