MASTER YOUR LOGIC BUILDING:Phase 4 - Level 3: Transformation

Mastering phase 4 - level 3: transformation concepts and implementation.

Phase 4 - Level 3: Transformation

Why Transform Lists?

Reading and searching are passive — you look but don't change. Transformation means reshaping data: flip the order, shift elements, swap two spots, or build a new list from the old one (squares, filters, etc.). This is where list logic starts to feel like real programming.

Two big questions before you code: Am I changing the original list, or making a new one?

What You'll Learn

  • Reverse a list in place (swap from both ends)
  • Rotate — shift every element left or right
  • Map — create a new list by transforming each value
  • When to modify arr vs build new_arr

Reverse In Place — Swap Mirrors

# [1, 2, 3, 4, 5] → swap pairs moving inward
for i in range(len(arr) // 2):
    j = len(arr) - 1 - i
    arr[i], arr[j] = arr[j], arr[i]

Think: index 0 swaps with last, index 1 with second-last — meet in the middle. Loop only to len // 2 so you don't swap twice.

Rotate Left by One — Save the First, Shift the Rest

first = arr[0]
for i in range(len(arr) - 1):
    arr[i] = arr[i + 1]   # each slot takes the next value
arr[-1] = first           # first value wraps to the end

[1,2,3,4] becomes [2,3,4,1] — like everyone in line takes one step forward and the front person moves to the back.

New List From Old — Don't Touch the Original

squared = []
for element in arr:
    squared.append(element * element)

When the problem says "create a new array," start with [] and append. Leave arr unchanged unless asked otherwise.

Step-by-Step Thinking Process

  1. In-place or new list? — Reverse/rotate usually change arr; squares/filters usually build new_arr.
  2. Draw the list before and after on paper — arrows show where each value moves.
  3. Boundary elements — first, last, and middle need explicit handling in rotations.
  4. Test small cases — [], one element, two elements.

Common Mistakes & Tips

  • Reversing with a loop to `len(arr)` — you swap back and undo your work; stop at len // 2.
  • Losing the first element in rotation — save arr[0] before shifting.
  • Modifying while iterating — avoid adding/removing items in the same loop you're traversing (confusing for beginners).
  • Tip: Python can do arr[::-1] to reverse — great to know, but learn the swap loop first; it builds index intuition.

Practice Focus

Try Reverse Array and New Array of Squares below. After reversing, print the list to confirm. After squaring, print both lists to see original vs transformed — that reinforces in-place vs new-list thinking.

Hands-on Examples

Reverse Array

# Take array
n = int(input("Enter array size: "))
arr = []
for i in range(n):
    arr.append(int(input(f"Enter element {i+1}: ")))

# Reverse array
print("Original array:", arr)
for i in range(len(arr) // 2):
    # Swap elements from both ends
    arr[i], arr[len(arr) - 1 - i] = arr[len(arr) - 1 - i], arr[i]

print("Reversed array:", arr)

Swap elements from both ends moving toward center. Loop only needs to go halfway (len(arr) // 2).