This rule raises an issue when an iterable is reversed using slicing, like [::-1], before being passed to set(), sorted(), or reversed().

Why is this an issue?

Reversing an iterable using slicing, likle [::-1], before passing it to set(), sorted(), or reversed() is unnecessary and inefficient. The slicing operation creates a new copy of the iterable in reverse order, which is not needed for the following reasons:

How to fix it

To fix these issues remove the redundant slicing operation: * set(iterable[::-1]): Replace with set(iterable). * sorted(iterable)[::-1]: Replace with sorted(iterable, reverse=True). * reversed(iterable[::-1]): Replace with iterable if it supports direct iteration, or iter(iterable) if it is a one-time iterator.

Code examples

Noncompliant code example

iterable = [1, 3, 2]
result = set(iterable[::-1]) # Noncompliant

Compliant solution

iterable = [1, 3, 2]
result = set(iterable)

Resources

Documentation