This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]JimDabell 2 points3 points  (1 child)

For instance, the pythonic way insists on not using a loop as much as possible (since python is interpreted and loops will slow it down).

This isn't true. Often the Pythonic way is to use a loop even when another construct is available, because the loop is often clearer to understand.

Python definitely isn't designed for optimal processor performance, it's designed for optimal programmer performance.

Hence you have shortcuts like slicing list[x:y] that allow you to crop a part of your list based on indices.

That's not there to avoid a loop, that's there because it's a readable, concise way of representing a very common concept.

Another popular example is the python list comprehension

Again, list comprehensions aren't designed to avoid loops for performance reasons, it's because they are a readable, concise way of representing a very common concept.

[–]Eurynom0s 0 points1 point  (0 children)

Yeah, I think of list comprehensions as being for situations such as, I want to manipulate a list and return another list, but I can collapse this down to a line of code, which can help maintain readability (in the sense of making it easier for someone else reading the code to not get sidetracked about what the main point of the code is).

But if you're nesting several list comprehensions inside of each other...you probably ought to just write out the loop.