Matrix (list-in-list), how to get a column?

Arvid Andersson arvid at linux.se
Thu Oct 21 17:19:04 CEST 2004


Good afternoon,

I have some data that looks like this:
data = [[1, 2], [3, 4], [5, 6]]

I want to send columns 1 and 2 to a function
as two variables, say "plot(col1,col2)".

I can get a row by data[r], but how do I get a
column? data[:][c] would have been my guess,
but that returns the same list as data[r].

I can solve this with two additional lists and
a for loop, but that seems like an ugly hack.
Five additional lines just seems clumsy.  :-)

Is there a more elegant solution than this?

col1 = []
col2 = []
for i in range(len(data)):
  col1 += [a[i][0]]
  col2 += [a[i][1]]


/Arvid Andersson




More information about the Python-list mailing list