list comprehension

cmdrrickhunter@yaho.com conrad.ammon at gmail.com
Mon Jul 3 06:03:59 EDT 2006


I woulkdn't interate at the same time.  zip takes two lists, and makes
a single list of tuples, not the other way around.  The easilest
solution is
feed_list = [ix.url for ix in feeds_list_select]
feed_id = [ix.id for ix in feeds_list_select]

Also, a big feature of list comprehension is it filters too... for
example
high_id_names = [ix.url for ix in feeds_list_select if ix.id > 500]
Sometimes list comprehensions just read nicely.
a wrote:
> hey guys
> this is gr8
> but in cheetah
> i use
> for test in $ix
> $test.url
> end for
> to iterate thru loop
>
> now how do i iterate feed_list and feed_id along with i,
> thanks a lot
>
> N = [(ix.url, ix.id) for ix in feeds_list_select]
>
> feed_list, feed_id = zip(*N)
>
> or just
>
> feed_list, feed_id = zip(*[(ix.url, ix.id) for ix in
> feeds_list_select])
>
> Simon Forman wrote:
> > a wrote:
> > > hi simon thanks for your reply
> >
> > You're most welcome
> >
> >
> > > what if i want to do this
> > > 	        feed_list=[]
> > > 		feed_id=[]
> > > 		for ix in feeds_list_select:
> > > 			global feeds_list
> > > 			global feeds_id
> > > 			feeds_list.append(ix.url)
> > > 		       feeds_id.append(ix.id)
> > >
> > > ie not one variable but more variables
> > > thanks
> >
> > in a case like this I would usually reach for the zip() function, with
> > the "varargs" * calling pattern
> >
> > N = [(ix.url, ix.id) for ix in feeds_list_select]
> >
> > feed_list, feed_id = zip(*N)
> >
> >
> > or just
> >
> > feed_list, feed_id = zip(*[(ix.url, ix.id) for ix in
> > feeds_list_select])
> >
> >
> > btw, please note that the global statements in your example are
> > unnecessary..  *totally* unnecessary.  :-D




More information about the Python-list mailing list