I want to print only a specified column of a 2D array. For example I can print a row [4,5,6] by doing:

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

But I want to print the column [2,5,8] by doing something like:

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

Is there a way of doing this?

You can write

print [row[1] for row in A]

or transpose the matrix

trans = zip(*A)
print trans[1]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.