[Tutor] Accessing Values of specific column in a 2D list or array

Alan Gauld alan.gauld at btinternet.com
Sun Sep 2 16:10:54 CEST 2007

"Varsha Purohit" <varsha.purohit at gmail.com> wrote

> grid = [[1,1,2,7,6,9],\
>          [,9,1,1,1,9,1],\
>          [8,1,2,0,0,4],\
>          [1,4,1,1,8,5]]

You don't need the '\' characters, the fact that the closing 
bracket has not been reached means Python will ignore 
the newline characters.

> how can i access to all the elements of the list from column no. 5.
> output should be like [6,9,0,8]...

Others have already shown but the easiest way to build a list 
from another list is via a list comprehension so:

col5 = [row[4] for row in grid]


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list