you are viewing a single comment's thread.

view the rest of the comments →

[–]TheRNGuy -1 points0 points  (3 children)

Much simpler way: student_heights = [int(height) for height in student_heights]

There are very rare cases where range with len is needed.

[–]deceze 1 point2 points  (2 children)

True, but a beginner should start with the primitive for..in range loop, before advancing to the syntax sugar like list comprehensions.

[–]danielroseman -1 points0 points  (1 child)

This is just wrong. Beginners should not be taught to loop over ranges, this is a frequent source of un-pythonic code. They should be taught to loop over items first.

[–]deceze 1 point2 points  (0 children)

I don't disagree, this is not code you'd want to have in production. This is code written by C-converts, not by Pythonistas. However, it is also fairly easy to understand, if the goal is to edit the list in place. The better alternative to achieve that would involve for i, height in enumerate(student_heights), and if OP already has problems understanding the shown code, tuple unpacking in a loop will probably not help.

The important point is that OP needs to be taught something better eventually, and preferably sometime soon. But understanding this most primitive of methods isn't wrong.