Notes

Source: 📖 Effective Python item 11


Negative indexing slices

Negative slicing can be used to make selections relative to the end of a list:


my_list = [1, 2, 3, 4]
sliced = my_list[:-1]

print(my_list)
print(sliced)

>>>
[1, 2, 3, 4]
[1, 2, 3]

my_list = [1, 2, 3, 4]
sliced = my_list[-2:]

print(my_list)
print(sliced)

>>>
[1, 2, 3, 4]
[3, 4]

See also: