python - Approach to iterating a list -


i'm pretty new python , working on existing code base.

i see pattern in code bit:

for in range(len(my_list)):     item = my_list[i]     # process item 

is there reason why should not simplified to:

for item in my_list:     # process item 

i not need index of item within for loop, other dereference list item itself. there problem latter not seeing?

there excellent reasons simplify second form; preferred pythonic method; use foo directly:

for foo in my_list:     # process item 

if index needed as well, use enumerate():

for i, foo in enumerate(my_list):     # process item 

creating for loop on range() of len() of list sign moved python language without for each construct, c, , have not yet gotten used how python for loops work.


Comments