Python list slicing 0:-1 leaving out last item

Viewed 26

I am trying to slice a list on certain conditions and if the condition is False I want to return the full list by only changing the [x:y] values.

list = [0,1,2,3,4,5]

if condition == True:
    y = 3
    x = 0
else:
    y = I_dont_know
    x = I_dont_know
return list[x:y]

Below is the current method I am trying to use but as you can see it is removing the last item of the list

>>> list = [0,1,2,3,4,5]
>>> len(list)
6
>>> len(list[0:-1])
5
>>> list[0:-1]
[0, 1, 2, 3, 4]
>>>

how do I stop the last item from getting sliced off and/or is there a better method?

0 Answers
Related