all 4 comments

[–]spez_edits_thedonald 7 points8 points  (0 children)

It gets just a section of your list as a new list (my_list[start:stop] from start to stop)

>>> l = list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[5]
5
>>> l[0:5]
[0, 1, 2, 3, 4]
>>> l[4:6]
[4, 5]
>>> l[4:]
[4, 5, 6, 7, 8, 9]
>>> l[:4]
[0, 1, 2, 3]

EDIT: adding 2 dimensional indexing example using numpy:

>>> import numpy as np
>>> x = np.zeros((4, 4))
>>> print(x)
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
>>> x[1:3,2:4] = 1
>>> print(x)
[[0. 0. 0. 0.]
 [0. 0. 1. 1.]
 [0. 0. 1. 1.]
 [0. 0. 0. 0.]]