Skip to content

Updating Lists

arr'[i] = value changes one element of the next list. Elements that are not written keep their current values, and every subscript on the right reads the current list, so a swap needs no temporary.

def reverse(xs: list[num]) -> list[num]:
init: arr = xs; lo = 0; hi = len(xs) - 1
while lo < hi:
arr'[lo] = arr[hi]
arr'[hi] = arr[lo]
lo' = lo + 1
hi' = hi - 1
return arr
print(reverse([1, 2, 3, 4, 5]))
Run in playground
Output
[5, 4, 3, 2, 1]