Can list slicing work without specifying the end?


#1

Question

In this exercise, the list slice shows a start and end point. Will a slice work without specifying the end?

Answer

Yes, it is possible to use a slice without specifying the end. In that case, the slice will return the list from the start point through the end of the list. This would be useful if you wanted to remove a known number of items from the start of the list and keep the remainder.

In the following example code, a list is sliced using a defined start and end point and then again specifying only the start.

colors = ['red', 'green', 'blue', 'yellow', 'orange']

# Values from index 1 to less than 4 = ['green', 'blue', 'yellow']
print(colors[1:4])

#  Start at index 1 till end = ['green', 'blue', 'yellow', 'orange']
print(colors[1:])