2

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

Then A[0][:] prints [1, 2, 3]

But why does A[:][0] print [1, 2, 3] again ?

It should print the column [1, 4, 7], shouldn't it?

EdChum
  • 376,765
  • 198
  • 813
  • 562
Shanks
  • 167
  • 8

7 Answers7

3

[:] 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]

Julien
  • 13,986
  • 5
  • 29
  • 53
2

When you don't specify a start or end index Python returns the entire array:

A[:] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
EngineerCamp
  • 661
  • 1
  • 6
  • 16
2

[:] matches the entire list.

So A[:] is the same as A. So A[0][:] is the same as A[0].

And A[0][:] is the same as A[0].

zezollo
  • 4,606
  • 5
  • 28
  • 59
2

Note that [:] just gives you a copy of all the content of the list. So what you are getting is perfectly normal. I think you wanted to use this operator as you would in numpy or Matlab. This does not do the same in regular Python.


A[0] is [1, 2, 3]

Therefore A[0][:] is also [1, 2, 3]


A[:] is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Therefore A[:][0] is [1, 2, 3]


If you wanted the first column you should try:

[e[0] for e in A]
# [1, 4, 7]
AdrienW
  • 3,092
  • 6
  • 29
  • 59
  • Thanks, that was fast ! I should use Numpy array then...print(A[:,0]) should print the column if A is Numpy. – Shanks Jul 29 '16 at 08:06
  • 2
    @Shanks the list comprehension is easier than using numpy though (numpy requires importing the library, that you have to extra install...). – zezollo Jul 29 '16 at 08:07
  • 1
    If you don't want to bother using _numpy_ just for this, you can still do the small trick I've added at the end. See what's best for your case! – AdrienW Jul 29 '16 at 08:07
2

A[:] returns a copy of the entire list. which is A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] A[:][0] Thus selects [1, 2, 3]. If you want the first column, do a loop:

col = []
for row in A:
    col.append(row[0])
Eric
  • 5,686
  • 2
  • 23
  • 36
2

Problem

A is not a 2-D list: it is a list of lists. In consideration of that:

  1. A[0] is the first list in A:

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

    Consequently, A[0][:]: is every element of the first list:

    >>> A[0][:]
    [1, 2, 3]
    
  2. A[:] is every element of A, in other words it is a copy of A:

    >>> A[:]
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    

    Consequently, A[:][0] is the first element of that copy of A.

    >>> A[:][0]
    [1, 2, 3]
    

Solution

To get what you want, use numpy:

>>> import numpy as np
>>> A = np.array( [[1, 2, 3], [4, 5, 6], [7, 8, 9]] )

A is now a true two-dimensional array. We can get the first row of A:

>>> A[0,:]
array([1, 2, 3])

We can similarly get the first column of A:

>>> A[:,0]
array([1, 4, 7])

`

John1024
  • 109,961
  • 14
  • 137
  • 171
2

A is actually a list of list, not a matrix. With A[:][0] You are accessing the first element (the list [1,2,3]) of the full slice of the list A. The [:] is Python slice notation (explained in the relevant Stack Overflow question).

To get [1,4,7] you would have to use something like [sublist[0] for sublist in A], which is a list comprehension, a vital element of the Python language.

Community
  • 1
  • 1