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

Alex Martelli aleaxit at
Thu Oct 21 17:41:15 CEST 2004

Arvid Andersson <arvid at> wrote:

> 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].

Right: data[:] makes a shallow copy of data, then you index into that --
no use.

col_c = [row[c] for row in data]

is probably the simplest, best and most idiomatic way to extract column
'c' from this kind of list-of-lists.

> Is there a more elegant solution than this?
> col1 = []
> col2 = []
> for i in range(len(data)):
>   col1 += [a[i][0]]
>   col2 += [a[i][1]]

Several -- this one has not a few small imperfections.  For example,
there is generally no need to iterate on indices:

    for row in data:
        col1 += [row[0]]

&c, would already be a small enhancement.  Moreover,
'col1 += [something]' is just a complicated way to express
col1.append(something), so you could further move to:

    for row in data:

&c.  And finally, you can recognize the pattern that:

anylist = []
for item in somewhere:
    anylist.append(<expression using item>)

is exactly what's meant by a list comprehension:

anylist = [<expression using item> for item in somewhere]

thus getting to the above-suggested

col1 = [row[0] for row in data]

and the like.  Since a list comprehension is an expression, you don't
have to give it a name if you don't want to; just pass it as the
argument in the function call.  Moreover, if you do this thing often,
much legibility at the cost of a tiny overhead could be had by having

def col(data, colindex):
    return [row[colindex] for row in data]

and using col(data, 0) and col(data, 1) as arguments to the function
you're calling.


More information about the Python-list mailing list