Programador Leigo
⚡ 60 segundos listas

Ordenar lista ao contrário com sorted() e reverse

Use sorted() com reverse=True para ordenação decrescente sem modificar a lista original.

sorted_reverse.py
notas = [7.5, 9.0, 8.2, 6.8, 9.5]

# Ordem decrescente (nova lista)
ranking = sorted(notas, reverse=True)
print(ranking)  # [9.5, 9.0, 8.2, 7.5, 6.8]

# Original intacta
print(notas)  # [7.5, 9.0, 8.2, 6.8, 9.5]

# .sort() modifica a lista original
notas.sort()
print(notas)  # [6.8, 7.5, 8.2, 9.0, 9.5]

Compartilhar