DEV Community

Discussion on: 11 Tips And Tricks To Write Better Python Code

Collapse
 
pat_loeber profile image
Patrick Loeber • Edited

Thanks for the extensive feedback!
Agree with 2, a lot of people use them too frequently and we should be careful here.

I also agree that range(len()) is fine and might be better suited for a beginner. I would never teach a beginner this method the first time I'm showing the for-loop. But as I said in the beginning, I wanted to show how the code can be more elegant and Pythonic. I'm not sure if better readability always has to mean that the code should be suited for a complete beginner. And the enumerate function shouldn't be too hard to understand once someone learned about it.

Collapse
 
andrewharpin profile image
Andrew Harpin

I disagree that it is more elegant, my personal opinion is good code can be read by a non coder, at least from an overview perspective.

With the other solution they would struggle as it requires inside knowledge, whereas range and len are much more obvious.

Thread Thread
 
aezore profile image
Jose Rodriguez

I also prefer enumerate anytime. range(len()) seems to defeat the motto of “simple over complex”. enumerate seems quite descriptive to me (non-English speaker) and otherwise once explained falls flat. If you are concern about variable unpacking...it makes sense to iterate over an index if you come from C, but python “for-each” approach seems to me more readable. IMHO ❤️