<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def extend_slice(s, shape, pad=1):
 r""" Adjust slice indices to include additional voxles around the slice. This function does bounds checking to ensure the indices don't extend outside the image. Parameters s : list of slice objects A list (or tuple) of N slice objects, where N is the number of dimensions in the image. shape : array_like The shape of the image into which the slice objects apply. This is used to check the bounds to prevent indexing beyond the image. pad : int The number of voxels to expand in each direction. Returns ------- slices : list of slice objects A list slice of objects with the start and stop attributes respectively incremented and decremented by 1, without extending beyond the image boundaries. Examples -------- Using the slices returned by ``find_objects``, set the first label to 3 [[3 0 0] [3 0 0] [0 0 2]] Next extend the slice, and use it to set the values to 4 [[4 4 0] [4 4 0] [4 4 2]] As can be seen by the location of the 4s, the slice was extended by 1, and also handled the extension beyond the boundary correctly. """

    pad = int(pad)
    a = []
    for i, dim in zip(s, shape):
        start = 0
        stop = dim
        if i.start - pad >= 0:
            start = i.start - pad
        if i.stop + pad < dim:
            stop = i.stop + pad
        a.append(slice(start, stop, None))
    return tuple(a)