Re: idiom for list looping

2009-07-29 Thread Xavier Ho
On Wed, Jul 29, 2009 at 9:17 PM, MRAB wrote: > I've just replaced the list comprehension with a generator expression. > > Oh, and that isn't in Python 2.3 I see. Generators are slightly newer, eh. Thanks! -- http://mail.python.org/mailman/listinfo/python-list

Re: idiom for list looping

2009-07-29 Thread MRAB
Xavier Ho wrote: On Wed, Jul 29, 2009 at 8:52 PM, MRAB > wrote: Slightly shorter: print '\n'.join("%s - %s" % p for p in enumerate(nomi)) :-) That's cool. Does that make the "list" a tuple? (not that it matters, I'm just curious!) I've just r

Re: idiom for list looping

2009-07-29 Thread superpollo
MRAB wrote: Xavier Ho wrote: superpollo wrote: for (i, e) in enumerate(nomi): print i, "-", e Just to be random: print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) This has one advantage: only print once. So it's slightly faster if you have a list of

Re: idiom for list looping

2009-07-29 Thread Xavier Ho
On Wed, Jul 29, 2009 at 8:52 PM, MRAB wrote: > Slightly shorter: > > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > > :-) > That's cool. Does that make the "list" a tuple? (not that it matters, I'm just curious!) I just tested for a list of 1000 elements (number in letters from 1 to 1

Re: idiom for list looping

2009-07-29 Thread MRAB
Xavier Ho wrote: superpollo wrote: for (i, e) in enumerate(nomi): print i, "-", e Just to be random: print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) This has one advantage: only print once. So it's slightly faster if you have a list of a large amount

Re: idiom for list looping

2009-07-29 Thread Xavier Ho
> > superpollo wrote: > >> >> for (i, e) in enumerate(nomi): >>print i, "-", e >> >> Just to be random: print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) This has one advantage: only print once. So it's slightly faster if you have a list of a large amount. -- http://mail.pyth

Re: idiom for list looping

2009-07-29 Thread MRAB
superpollo wrote: hi clp. i want to get from here: nomi = ["one", "two", "three"] to here: 0 - one 1 - two 2 - three i found two ways: first way: for i in range(len(nomi)): print i, "-", nomi[i] or second way: for (i, e) in enumerate(nomi): print i, "-", e which one is "better"?

Re: idiom for list looping

2009-07-29 Thread David Roberts
To the best of my knowledge the second way is more pythonic - the first is a little too reminiscent of C. A couple of notes: - you don't need the parentheses around "i, e" - if you were going to use the first way it's better to use xrange instead of range for iteration -- David Roberts http://da.v