Shanks - 10 months ago 36

Python Question

Suppose if

`A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]`

Then

`A[0][:]`

`[1, 2, 3]`

But why does

`A[:][0]`

`[1, 2, 3]`

It should print the column

`[1, 4, 7]`

Answer

`[:]`

is equivalent to copy.

`A[:][0]`

is the first row of a copy of A.
`A[0][:]`

is a copy of the first row of A.

The two are the same.

To get the first column: `[a[0] for a in A]`

Or use numpy and `np.array(A)[:,0]`

Source (Stackoverflow)