Why this code doesn’t work as expected?
I know there are other ways to reverse list using recursion
but I am interested particularly in why this method doesn’t work.
def reverse(nums):
if len(nums) < 2:
return
nums[0], nums[-1] = nums[-1], nums[0]
reverse(nums[1:-1])
A = [1, 2, 3, 4, 5, 6]
reverse(A)
print(A)
I am expecting the output [6 5 4 3 2 1]
.
New contributor
Neeraj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.