Exercise: Slicing lists 1

3.6.3.4. Exercise: Slicing lists 1#

Here is a list:

list_of_strings = ["zero", "one", "two", "three", "four", "five"]

What would be the code to transform list_of_strings into:

["five", "four", "three", "two", "one", "zero"]

Tip

You may want to read on negative indexing again.

Hide code cell content
# We start at the last element of list_of_strings.
# We can use negative indexing to get this element: -1
# Therefore, the first number of the slice (start index) is -1.

# We will go backward through the list.
# Therefore, the third number of the slice (increment) is -1.

# We go through all the list. There is no ending point.
# Therefore, there is no second number in the slice.

list_of_strings[-1::-1]
['five', 'four', 'three', 'two', 'one', 'zero']