[Python] Understanding output of slicing operation

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have a list object which looks as below:

    lst = [50,34,98,8,10]


    The output for the statement print(lst[:5:-2]) is coming as blank []

    My understanding is that print(lst[:5:-2]) translates to print(lst[-1:5:-2])

    This means:

    • start from index -1 # this corresponds to value 10
    • stop at index 4 # this corresponds to value 10
    • perform increment of -2

    As start and stop are pointing to 10 so I am expecting the output to be 10 here.

    Continue reading...

Compartilhe esta Página