There are several ways to reverse a list in Python:
Using the reverse() method (in-place modification):
This method modifies the original list directly, reversing its elements without creating a new list.
Python
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list) # Output: [5, 4, 3, 2, 1]
Using slicing [::-1] (creates a new list):
This method creates a new reversed copy of the list while leaving the original list unchanged.
Python
original_list = [1, 2, 3, 4, 5]
reversed_list = original_list[::-1]
print(reversed_list) # Output: [5, 4, 3, 2, 1]
print(original_list) # Output: [1, 2, 3, 4, 5] (original list remains unchanged)
Using the reversed() function (returns an iterator):
The reversed() function returns an iterator that yields elements in reverse order. To get a list, you need to convert it using list().
Python
my_list = [1, 2, 3, 4, 5]
reversed_iterator = reversed(my_list)
reversed_list = list(reversed_iterator)
print(reversed_list) # Output: [5, 4, 3, 2, 1]
Nesta página do site você pode assistir ao vídeo on-line REVERSE A LIST IN PYTHON(heck description) duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Oculustechnologies 18 Julho 2025, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 2,088 vezes e gostou 9 espectadores. Boa visualização!