Session Ready
Exercise

Slicing multiple list elements

Slicing operations on a list are used to subset multiple elements from a list. The syntax for list slicing is as follows:

list[start:end]

Remember, this syntax indicates subsetting all elements from the start and up to but not including the end element.

Also, you can use extended slicing to efficiently select multiple elements from a list. For example, the following command returns all elements from the list except the first (at index 0):

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]

Similarly, the following command returns all elements from the list except the last two:

x[:-2]

[1, 2, 3]
Instructions 1/2
undefined XP
  • 1
  • 2

Use list slicing to subset the last two elements from names.