Usually if lists are short, it would be easy to find out the indexes for the slicing method list[start:end] .
However, if lists happen to be extremely long, finding the indexes for the desired items becomes difficult. Is there a way to address this problem, like a more convenient method?
I don’t think there is one size fits all solution, it depends. You might use the index method to find the index. But this might be problematic if you have duplicate values.
that [::-1] means go through the entire list, and the -1 means do it backward
x = ['a', 'b', 'c', 'd', 'e', 'f']
# look at entire list, go backward
y = x[::-1]
print(y)
# look at entire list, use every 2nd entry
z = x[::2]
print(z)
x = list(range(11))
print(x)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# From first element to end of list with step size of 1
print(x[:])
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# From last element to start of list with step of -1 (reverse direction)
print(x[::-1])
# [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# From element at index 4 to end of list
print(x[4:])
# [4, 5, 6, 7, 8, 9, 10]
# From element at index 2 (included) to element at index 9 (not included)
# with step of 2
print(x[2:9:2])
# [2, 4, 6, 8]
# From element at index 4 to end of list with step of 3
print(x[4::3])
# [4, 7, 10]
# From first element to element at index -5 (not included) with step of 1
print(x[:-5])
# [0, 1, 2, 3, 4, 5]
# From element at index -5 (included) to end of list with step of 1
print(x[-5:])
# [6, 7, 8, 9, 10]
# From element at index -5 (included) to element at index 9 (not included)
# with step of 1
print(x[-5:9])
# [6, 7, 8]
# From element at index -9 (included) to element at index -4 (not included)
# with step of 1
print(x[-9:-4])
# [2, 3, 4, 5, 6]
# From element at index -3 (included) to element at index -1 (not included)
# with step of 1
print(x[-3:-1])
# [8, 9]
# From element at index 1 (included) to start of list
# with step of -1 (reverse direction)
print(x[1::-1])
# [1, 0]
# From element at index -1 (included) to start of list
# with step of -1 (reverse direction)
print(x[-1::-1])
# [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# From element at index -1 (included) to element at index -6 (not included)
# with step of -1 (reverse direction)
print(x[-1:-6:-1])
# [10, 9, 8, 7, 6]
# From element at index -3 (included) to element at index -5 (not included)
# with step of 1
print(x[-3:-5])
# []
# With a step of +1, it is not possible to start at index -3 and reach index -5
# From element at index 4 (included) to element at index 2 (not included)
# with step of 1
print(x[4:2])
# []
# With a step of +1, it is not possible to start at index 4 and reach index 2
With the above in mind, your example:
num_list = [7, 6, 5, 4, 3, 2]
# From element at index -1 (included) to element at index -3 (not included)
# with step of 1
print(num_list[-1:-3])
# []
# With a step of +1, it is not possible to start at index -1 and reach index -3
# From element at index -1 (included) to element at index -3 (not included)
# with step of -1 (reverse direction)
print(num_list[-1:-3:-1])
# [2, 3]
You should create your own lists and play around with different combinations for upper and lower bounds and steps to further your understanding of slicing in Python.