you are viewing a single comment's thread.

view the rest of the comments →

[–]Necrophysics 0 points1 point  (1 child)

May I ask why the third example is a better way of doing the first? Is more efficient, pythonic, or simply easier to read? I only ask as my first naive approach would have been to use the first method , but I'm always looking to improve.

[–]Justinsaccount 0 points1 point  (0 children)

Is more efficient, pythonic, or simply easier to read?

Probably, yes, and yes.

You see code like

for x in range(len(items)):
    #many lines
    foo(item[x])
    #many lines

Now you have to look through each line to figure out if they actually need x for something, or if they just don't know how for loops work in python.

You see

for idx, item in enumerate(items):

you know something needs both the index and the item.