Python has an interesting method called slice. This function generates a slice which is a set of indices between start and stop every step elements. s:
letters = ["a", "b", "c", "d", "e", "f", "g"] first_3_letters = slice(3) assert letters[first_3_letters] == ["a", "b", "c"] last_2_letters = slice(-1, -3, -1) assert letters[last_2_letters] == ["g", "f"]
A synonym for slice() is colon-based syntax like [start:stop:step]:
first_2_letters_colon_syntax = letters[0:3:1] assert first_2_letters_colon_syntax == ["a", "b", "c"]