1a[start:stop] # items start through stop-1
2a[start:] # items start through the rest of the array
3a[:stop] # items from the beginning through stop-1
4a[:] # a copy of the whole array
5Example:
6>>> a = [1, 2, 3, 4, 5, 6, 7, 8]
7>>> a[1:4]
8[2, 3, 4]
1# Basic syntax:
2your_list[start:stop:step]
3
4# Note, Python is 0-indexed
5# Note, start is inclusive but stop is exclusive
6# Note, if you leave start blank, it defaults to 0. If you leave stop
7# blank, it defaults to the length of the list. If you leave step
8# blank, it defaults to 1.
9# Note, a negative start/stop refers to the index starting from the end
10# of the list. Negative step returns list elements from right to left
11
12# Example usage:
13your_list = [0, 1, 2, 3, 4, 5]
14your_list[0:5:1]
15--> [0, 1, 2, 3, 4] # This illustrates how stop is not inclusive
16
17# Example usage 2:
18your_list = [0, 1, 2, 3, 4, 5]
19your_list[::2] # Return list items for even indices
20--> [0, 2, 4]
21
22# Example usage 3:
23your_list = [0, 1, 2, 3, 4, 5]
24your_list[1::2] # Return list items for odd indices
25--> [1, 3, 5]
26
27# Example usage 4:
28your_list = [0, 1, 2, 3, 4, 5]
29your_list[4:-6:-1] # Return list items from 4th element from the left to
30# the 6th element from the right going from right to left
31--> [4, 3, 2, 1]
32# Note, from the right, lists are 1-indexed, not 0-indexed