Slicing syntax in Python is very flexible. Let’s look at a few more problems we can tackle with slicing.

Take the list fruits as our example:

fruits = ["apple", "cherry", "pineapple", "orange", "mango"]

If we want to select the first n elements of a list, we could use the following code:


For our fruits list, suppose we wanted to slice the first three elements.

The following code would start slicing from index 0 and up to index 3. Note that the fruit at index 3 (orange) is not included in the results.


Would output:

['apple', 'cherry', 'pineapple']

We can do something similar when we want to slice the last n elements in a list:


For our fruits list, suppose we wanted to slice the last two elements.

This code slices from the element at index -2 up through the last index.


Would output:

['orange', 'mango']

Negative indices can also accomplish taking all but n last elements of a list.


For our fruits example, suppose we wanted to slice all but the last element from the list.

This example starts counting from the 0 index up to the element at index -1.


Would output:

['apple', 'cherry', 'pineapple', 'orange']

Let’s practice some of these extra slicing techniques!



Create a new list called last_two_elements containing the final two elements of suitcase.

Print last_two_elements to see your result.


Create a new list called slice_off_last_three containing all but the last three elements.

Print slice_off_last_three to see your result.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?