Friday, October 16, 2026

Reverse a list in Python

 Reverse a List in Python
------------------------
# Option 1: Reverse a List in Python Using the reverse() Method

my_list = [1, 2, 3, 4, 5]
my_list.reverse()  # In-place reversal
print(my_list)  # Output: [5, 4, 3, 2, 1]


#We use the reverse() method, which modifies the original list directly and reverses the order of its elements.
#This method performs the reversal in-place without creating a new list, making it memory efficient.

#Option 2: Reverse a List Using Slicing in Python

my_list = [10, 20, 30, 40, 50]
reversed_list = my_list[::-1]  # Reversing using slicing
print(reversed_list)  # Output: [50, 40, 30, 20, 10]

#This creates a new list with the elements in reverse order, while keeping the original list unchanged.
#When we print reversed_list, the output will be [50, 40, 30, 20, 10]. This method is simple and does not affect the original list.

#Option 3: Reverse a List Using the reversed() Function in Python

my_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(my_list))  # reversed() returns an iterator
print(reversed_list)  


#Option 4: Reverse a List in Python Using a For Loop

my_list = [15, 30, 45, 60]
reversed_list = []
index = len(my_list) - 1
while index >= 0:
    reversed_list.append(my_list[index])  # Append elements from the end
    index -= 1
print(reversed_list)

'''
Code Explanation:

Here is a step-by-step breakdown of the execution:
1. my_list = [1, 2, 3, 4, 5]: Initializes the starting list.
2. reversed_list = []: Creates an empty list to store the reversed elements.
3. for item in my_list:: Starts a loop that processes each number from left to right (1, then 2, then 3, etc.).
4. reversed_list.insert(0, item): This is the core logic.
    • Step 1 (item = 1): Inserts 1 at index 0. → reversed_list becomes [1].
    • Step 2 (item = 2): Inserts 2 at index 0, pushing 1 to the right. → reversed_list becomes [2, 1].
    • Step 3 (item = 3): Inserts 3 at index 0, pushing the others down. → reversed_list becomes [3, 2, 1].
    • This repeats until the loop finishes, resulting in [5, 4, 3, 2, 1].

''' 


#Option 5: Reverse a List Using a While Loop in Python


my_list = [15, 30, 45, 60]
reversed_list = []
index = len(my_list) - 1
while index >= 0:
    reversed_list.append(my_list[index])  # Append elements from the end
    index -= 1
print(reversed_list)  

'''
Code Explanation:
1. my_list = [15, 30, 45, 60]: Initializes the starting list.
2. reversed_list = []: Creates an empty list to store the reversed elements.
3. index = len(my_list) - 1: Sets the starting index to 3 (the last index of my_list, which points to 60).
4. while index >= 0:: Starts a loop that runs as long as the index is valid.
    • Step 1 (index = 3): Appends my_list[3] (60) to reversed_list. Index becomes 2.
    • Step 2 (index = 2): Appends my_list[2] (45) to reversed_list. Index becomes 1.
    • Step 3 (index = 1): Appends my_list[1] (30) to reversed_list. Index becomes 0.
    • Step 4 (index = 0): Appends my_list[0] (15) to reversed_list. Index becomes -1.
5. The loop terminates because -1 >= 0 is false. reversed_list outputs [60, 45, 30, 15].
'''



my_list = [1, 2, 3]

# Append treats the string as one item
my_list.append("cat") 

print(my_list)
# Result: [1, 2, 3, 'cat']

# Extend unpacks the string character by character!
my_list.extend("dog") 
# Result: [1, 2, 3, 'cat', 'd', 'o', 'g']
print(my_list)


No comments:

Post a Comment