Slicing manually using for and while loops

Let's now implement slicing using loops.

In [1]:
def manual_slice(L, i, j, step):
    '''Return L[i:j:step]'''
    newL = []
    for ind in range(i, j, step):
        newL.append(L[ind])
    return newL
In [2]:
def manual_slice_while(L, i, j, step):
    '''Return L[i:j:step]. Assume step>0'''
    newL = []
    ind = i
    #assuming positive step
    while ind < j:
        newL.append(L[ind])
        ind += step
    return newL