How to Reverse a List
To reverse the order of elements in a list, you can use the reverse()
method or slicing [::-1]
.
Example: Reversing a List
# Reversing a list
numbers = [1, 2, 3, 4, 5]
numbers.reverse() # Using reverse()
print(numbers)
Output
[5, 4, 3, 2, 1]
Explanation: The reverse()
method reverses the elements of the list in place, changing the original order.