Is it alright to call len() in a loop's conditional statement?
- by DormoTheNord
In C, it is considered bad practice to call strlen like this:
for ( i = 0; strlen ( str ) != foo; i++ )
{
// stuff
}
The reason, of course, is that it is inefficient since it "counts" the characters in a string multiple times.
However, in Python, I see code like this quite often:
for i in range ( 0, len ( list ) ):
# stuff
Is this bad practice? Should I store the result of len() in a variable and use that?