Skip to content

Instantly share code, notes, and snippets.

@mekicha
Created January 26, 2018 17:39
Show Gist options
  • Save mekicha/37ad3f6442a6b1f2632e08d644a71485 to your computer and use it in GitHub Desktop.
Save mekicha/37ad3f6442a6b1f2632e08d644a71485 to your computer and use it in GitHub Desktop.
A simple python function to reverse a list in place.
def reverse_it(arr):
if not arr:
return
high, low = len(arr) - 1 , 0
while high > low:
arr[low], arr[high] = arr[high], arr[low]
high, low = high - 1, low + 1
return arr
a = [1, 2, 3, 4, 5]
b = [ 10, 9, 8, 7, 6]
print(reverse_it(a)) # [5, 4, 3, 2, 1]
print(reverse_it(b)) # [6, 7, 8, 9, 10]
print(reverse_it([1, 2])) # [2, 1]
print(reverse_it([1])) # [1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment