Back to all reviewers

Optimize iteration patterns

apache/mxnet
Based on 6 comments
Python

When iterating through collections, choose the most efficient iteration pattern based on what information you actually need. If you only need the elements without using their indices, use direct iteration instead of enumerate:

Algorithms Python

Reviewer Prompt

When iterating through collections, choose the most efficient iteration pattern based on what information you actually need. If you only need the elements without using their indices, use direct iteration instead of enumerate:

# Instead of this (inefficient):
for _, item in enumerate(collection):
    process(item)

# Do this (efficient and clearer):
for item in collection:
    process(item)

This optimization removes unnecessary index variable allocation and makes the code more readable. The principle applies to any iterative algorithm - don’t compute values you won’t use. Similarly, when testing object capabilities (like whether an object is iterable), check for specific attributes (hasattr(obj, '__iter__')) rather than calling functions that might have side effects like prefetching data.

6
Comments Analyzed
Python
Primary Language
Algorithms
Category

Source Discussions