You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Forbid looping constructs over indexes that are equivalent to slice operations.
Thesis
I recently encountered a point in my code where I was using a comprehension to iterate over a range to index into a list. Here is a basic example that currently passes all checks:
my_list = [1, 2, 3, 4, 5]
# Wrong:
my_sublist = [my_list[index] for index in range(1, 4)]
# Right:
my_sublist = my_list[1:4]
A trickier example closer to what I found:
some_text = 'Python!'
an_index = 4
# Wrong (but fails on Jones complexity):
subset = {some_text[index] for index in range(an_index - 1, an_index - 4, -1)}
# Right:
subset = set(some_text[an_index - 3:an_index])
Reasoning
These are redundant, probably slower, and harder to read.
The text was updated successfully, but these errors were encountered:
Rule request
Forbid looping constructs over indexes that are equivalent to slice operations.
Thesis
I recently encountered a point in my code where I was using a comprehension to iterate over a
range
to index into a list. Here is a basic example that currently passes all checks:A trickier example closer to what I found:
Reasoning
These are redundant, probably slower, and harder to read.
The text was updated successfully, but these errors were encountered: