5 Respostas
+ 1
I don't know about python nor it syntax but in any language, to optimize your code you shouldn't put any function in the declaration of the loop.
Here is an exemple :
Prefer =>
size = array.length
For(i=0 ; i < size ; i++)
To =>
For(i=0 ; i < array.length ; i++)
If you put a function inside of the loop declaration, it gonna be called each iteration, the result wont be stored by itself.
+ 1
Hope someone will be of more help then :)
The question is actually quite interesting (even for a non python dev as myself)
+ 1
Firstly, I would suggest to avoid 'for loops' as much as possible and use list comprehension instead.
In case we have to use 'for loops', found this article on optimizing the loop. Have tested the codes & it works perfectly
https://nyu-cds.github.io/JUMP_LINK__&&__python__&&__JUMP_LINK-performance-tips/08-loops/
+ 1
Bishu Giri Thank you, but I could not able to use list comprehension for every problem. I have read this article already, thank you again.