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

Orri Ganel singingxduck at gmail.com
Wed Jan 12 23:44:07 CET 2005


Vincent Wan wrote:

> If I have a list stuff = [[0,sdfsd,wrtew], [1, rht,erterg]] whats the  
> most concise way to print the first elements:
>
> [0, 1]
>
> if tried    print stuff[:][0]  but that just prints the whole list.
>
> Vincent
>
> ------------------------------------------------------------------------ 
> --------------
> PhD Candidate
> Committee on the Conceptual and Historical Studies of Science
> University of Chicago
>
> PO Box 73727
> Fairbanks, AK 99707
>
> wan AT walrus DOT us (change CAPS to @ and . )
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

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

List comprehensions - cool stuff ;-)

-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.



More information about the Tutor mailing list