In trying to select the first (when counting from zero) column in a 2D 4x4 array, I wrote the following script:
import numpy
a4x4=[list(range(4*i,4*(i+1))) for i in list(range(4))]
print(a4x4)
print(a4x4[:,1])
The array seems to be alright:
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]
but instead of
[1, 5, 9, 13]
for the second print, I get this error:
TypeError: list indices must be integers, not tuple
Why does this error appear, what is going wrong?
listcalls.for i in list(range(4))will give you the exact same results as justfor i in range(4), except that it will waste time and memory building a list with the same elements as the range that you already had.