my_list=['a','b','c','d','x','y','z']
my_list.reverse()
print(my_list)
Output
['z', 'y', 'x', 'd', 'c', 'b', 'a']
Reversing Numeric Lists:numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers) # Output: [5, 4, 3, 2, 1]
Use Case: Undoing Sorting: If you've sorted a list and want to restore its original order:items = ['apple', 'banana', 'cherry']
items.sort() # ['apple', 'banana', 'cherry']
items.reverse() # ['cherry', 'banana', 'apple']
Reversing String Lists:
words = ['one', 'two', 'three']
words.reverse()
print(words) # Output: ['three', 'two', 'one']
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list) # Output: [5, 4, 3, 2, 1]
my_list = [4, 2, 9, 1]
my_list.sort()
my_list.reverse()
print(my_list) # Output: [9, 4, 2, 1]
my_list = [1, "apple", 3.14]
my_list.reverse()
print(my_list) # Output: [3.14, 'apple', 1]
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.