Reversing a list in Python can be done using the reverse()
method or by utilizing slicing. Here are two examples:
Example 1:
my_list = [1, 2, 3, 4, 5] my_list.reverse() print(my_list)
Output:
[5, 4, 3, 2, 1]
Example 2:
fruits = ['apple', 'banana', 'orange'] reversed_fruits = fruits[::-1] print(reversed_fruits)
Output:
[‘orange’, ‘banana’, ‘apple’]