Question on loops

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Sun Mar 7 21:10:57 EST 2004


> From: Daniel H Neilson
> 
> i = 0
> for line in file:
>     if i == 100:
>         action
>         i = 0
>     else:
>         i += 1
>     process(line)
> 
> But this doesn't feel like a very python-esque solution to me, and is
> not very elegant in any case. What would be a better way to do it?

If you're using Python 2.3.x

for i, line in enumerate(file):
    if i % 100 == 0:
        action

    process(line)

Tim Delaney




More information about the Python-list mailing list