is this pythonic?

Bill Mill bill.mill at gmail.com
Wed Jul 20 16:30:10 EDT 2005


On 7/20/05, Simon Brunning <simon.brunning at gmail.com> wrote:
> On 7/20/05, Mage <mage at mage.hu> wrote:
> > Or is there better way?
> >
> > for (i, url) in [(i,links[i]) for i in range(len(links))]:
> 
> for i, url in enumerate(links):
> 

+2 for creating seeing a need and crafting a reasonable solution, but
-1 for not reading the section on builtins to see if it existed
already.

(As for its pythonicity, I would have recommended isolating it into a
function and making it a generator:

def my_enumerate(enumerable):
    i = 0
    for elt in enumerable:
        yield (i, elt)
        i += 1

for i, url in my_enumerate(links):

but it's not too bad as it is. Also, my function is completely
untested - it's close to right though.)

Peace
Bill Mill



More information about the Python-list mailing list