[Tutor] RE:

Orri Ganel singingxduck at gmail.com
Tue Jan 18 09:37:50 CET 2005


Gopinath V, ASDC Chennai wrote:

> 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!
>
>
>    This is fine.i just want to know if row is a reserve word ?
> or is it a built in function
>  in IDLe environment .. the word row is not highlighted ,what data 
> type is (row)
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>
That'll teach me to read the whole question.  In the above list 
comprehension, stuff is the defined list.  row is a variable created by 
the list comprehension.  Basically, this is another way of saying:

for row in stuff:
    print row[0]

or

for i in range(len(stuff)):
    print stuff[i][0]

In this case, row's type is whatever type that element in stuff is 
(which is a list). Ie:

 >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']]
 >>> print [(row[0], type(row), type(row[0])) for row in stuff]
[(0, <type 'list'>, <type 'int'>), (1, <type 'list'>, <type 'int'>)]

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050118/83c7ec19/attachment-0001.htm


More information about the Tutor mailing list