FAQ: Working with Lists in Python - Slicing Lists II

The element at the stop index is not included.

Can you post an example that replicates the behavior described by you?

Consider the example:

letters = ["A", "B", "C", "D", "E", "F", "G", "H"]

#
print(letters[:5])
# ['A', 'B', 'C', 'D', 'E']
# The element at index 5 is "F"
# All elements before "F" are included
# Non-negative indexes begin from 0, i.e.
# the first element "A" is at index 0 

#
print(letters[:-5])
# ['A', 'B', 'C']
# The element at index -5 is "D"
# All elements before "D" are included
# Negative indexes begin from -1, i.e.
# the last element "H" is at index -1

Here are a few other examples of slicing:

https://discuss.codecademy.com/t/faq-working-with-lists-in-python-slicing-lists/371504/12

1 Like