Ir al contenido

Order by before where (ARCH2076)

Esta página aún no está disponible en tu idioma.

Key python.performance.orderby-before-where · Severity warning · Category performance

Sorting before filtering wastes work by ordering elements that will be discarded. Filter first, then sort the smaller remaining set.

def top_active(users):
ordered = sorted(users, key=lambda u: u.score, reverse=True)
return [u for u in ordered if u.active][:10]
def top_active(users):
active = [u for u in users if u.active]
return sorted(active, key=lambda u: u.score, reverse=True)[:10]

Filter first and apply the sort only to the smaller remaining set.