I'm looking to reverse a list using recursion in python. I succeeded in doing so but the output isn't exactly what I wanted. trying to figure out where i'm going wrong.
def revlist(ls):
newlist = []
n = len(ls)
if n == 1:
return ls[-1]
else:
return ls[-1],revlist(ls[:-1])
This is my output.
revlist([1,2,3,4])
(4, (3, (2, 1)))
What i'm really hoping to get is this:
revlist([1,2,3,4])
(4,3,2,1)