The answers above don't discuss slice assignment. To understand slice assignment, it's helpful to add another concept to the ASCII art:

` +---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ Slice position: 0 1 2 3 4 5 6 Index position: 0 1 2 3 4 5 >>> p = ['P','y','t','h','o','n'] # Why the two sets of numbers: # indexing gives items, not lists >>> p[0] 'P' >>> p[5] 'n' # Slicing gives lists >>> p[0:1] ['P'] >>> p[0:2] ['P','y'] `

One heuristic is, for a slice from zero to n, think: "zero is the beginning, start at the beginning and take n items in a list".

`>>> p[5] # the last of six items, indexed from zero 'n' >>> p[0:5] # does NOT include the last item! ['P','y','t','h','o'] >>> p[0:6] # not p[0:5]!!! ['P','y','t','h','o','n'] `

Another heuristic is, "for any slice, replace the start by zero, apply the previous heuristic to get the end of the list, then count the first number back up to chop items off the beginning"

`>>> p[0:4] # Start at the beginning and count out 4 items ['P','y','t','h'] >>> p[1:4] # Take one item off the front ['y','t','h'] >>> p[2:4] # Take two items off the front ['t','h'] # etc. `

The first rule of slice assignment is that since slicing *returns* a list, slice assignment *requires* a list (or other iterable):

`>>> p[2:3] ['t'] >>> p[2:3] = ['T'] >>> p ['P','y','T','h','o','n'] >>> p[2:3] = 't' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only assign an iterable `

The second rule of slice assignment, which you can also see above, is that whatever portion of the list is returned by slice indexing, that's the same portion that is changed by slice assignment:

`>>> p[2:4] ['T','h'] >>> p[2:4] = ['t','r'] >>> p ['P','y','t','r','o','n'] `

The third rule of slice assignment is, the assigned list (iterable) doesn't have to have the same length; the indexed slice is simply sliced out and replaced en masse by whatever is being assigned:

`>>> p = ['P','y','t','h','o','n'] # Start over >>> p[2:4] = ['s','p','a','m'] >>> p ['P','y','s','p','a','m','o','n'] `

The trickiest part to get used to is assignment to empty slices. Using heuristic 1 and 2 it's easy to get your head around *indexing* an empty slice:

`>>> p = ['P','y','t','h','o','n'] >>> p[0:4] ['P','y','t','h'] >>> p[1:4] ['y','t','h'] >>> p[2:4] ['t','h'] >>> p[3:4] ['h'] >>> p[4:4] [] `

And then once you've seen that, slice assignment to the empty slice makes sense too:

`>>> p = ['P','y','t','h','o','n'] >>> p[2:4] = ['x','y'] # Assigned list is same length as slice >>> p ['P','y','x','y','o','n'] # Result is same length >>> p = ['P','y','t','h','o','n'] >>> p[3:4] = ['x','y'] # Assigned list is longer than slice >>> p ['P','y','t','x','y','o','n'] # The result is longer >>> p = ['P','y','t','h','o','n'] >>> p[4:4] = ['x','y'] >>> p ['P','y','t','h','x','y','o','n'] # The result is longer still `

Note that, since we are not changing the second number of the slice (4), the inserted items always stack right up against the 'o', even when we're assigning to the empty slice. So the position for the empty slice assignment is the logical extension of the positions for the non-empty slice assignments.

Backing up a little bit, what happens when you keep going with our procession of counting up the slice beginning?

`>>> p = ['P','y','t','h','o','n'] >>> p[0:4] ['P','y','t','h'] >>> p[1:4] ['y','t','h'] >>> p[2:4] ['t','h'] >>> p[3:4] ['h'] >>> p[4:4] [] >>> p[5:4] [] >>> p[6:4] [] `

With slicing, once you're done, you're done; it doesn't start slicing backwards. In Python you don't get negative strides unless you explicitly ask for them by using a negative number.

`>>> p[5:3:-1] ['n','o'] `

There are some weird consequences to the "once you're done, you're done" rule:

`>>> p[4:4] [] >>> p[5:4] [] >>> p[6:4] [] >>> p[6] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range `

In fact, compared to indexing, Python slicing is bizarrely error-proof:

`>>> p[100:200] [] >>> p[int(2e99):int(1e99)] [] `

This can come in handy sometimes, but it can also lead to somewhat strange behavior:

`>>> p ['P', 'y', 't', 'h', 'o', 'n'] >>> p[int(2e99):int(1e99)] = ['p','o','w','e','r'] >>> p ['P', 'y', 't', 'h', 'o', 'n', 'p', 'o', 'w', 'e', 'r'] `

Depending on your application, that might... or might not... be what you were hoping for there!

Below is the text of my original answer. It has been useful to many people, so I didn't want to delete it.

`>>> r=[1,2,3,4] >>> r[1:1] [] >>> r[1:1]=[9,8] >>> r [1, 9, 8, 2, 3, 4] >>> r[1:1]=['blah'] >>> r [1, 'blah', 9, 8, 2, 3, 4] `

This may also clarify the difference between slicing and indexing.