[Tutor] what's a concise way to print the first elements in a nested list

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Jan 13 02:13:08 CET 2005



On Wed, 12 Jan 2005, Orri Ganel wrote:

>  >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']]
>  >>> stuff
> [[0, 'sdfsd', 'wrtew'], [1, 'rht', 'erterg']]
>  >>> print [stuff[i][0] for i in range(len(stuff))]
> [0, 1]


Hi Orri,

An alternative way to write this is:

###
print [row[0] for row in stuff]
###

which extracts the first element out of every "row" sublist in 'stuff'.


Best of wishes!



More information about the Tutor mailing list