Think of it this way. You are currently having a 1 x 5
dimension array. In other words, you have five elements in the array. That is why no matter whether you do print(arr[:][1])
or print(arr[1][:])
, you will still get [11, 3]
.
The first solution refers to the whole list and then find the 2nd element in the array. The latter solution refers to the 2nd element in the array, then search for all elements in the 2nd element(if the 2nd element is a list)
Perhaps it will be easier if you have a 2 x 5
dimension array.
arr = [[[10, 2], [11, 3], [12, 4], [13, 4], [14, 5]],
[[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]]
Here, if you do print(arr[:][1])
, you will get
[[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]
which is expected as it searches the whole array and returns the 2nd element (the 2nd nested list) in the array.
If you do print(arr[1][:])
, you will get
[[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]
which will be the same too, because it again looks for the 2nd element(the 2nd nested list) in the array first and searches for all the elements in the nested list.
Now, back to what you want, if you only want to have every first element in the array, you can do this.
for i in range(len(arr)):
print(arr[i][0])
[10, 2]
[12, 6]
Based on the loop above, you are looking for every row of the nested list in the array and return the first element in the nested list.