Skip to content

Instantly share code, notes, and snippets.

@hackjoy
Created February 8, 2013 01:33
Show Gist options
  • Select an option

  • Save hackjoy/4735883 to your computer and use it in GitHub Desktop.

Select an option

Save hackjoy/4735883 to your computer and use it in GitHub Desktop.
Reverse every element within a list including nested lists.
def is_list(p):
return isinstance(p, list)
def deep_reverse(p):
if is_list(p):
result = []
for i in range (len(p) - 1, -1, -1):
result.append(deep_reverse(p[i]))
return result
else:
return p
p = [1, [2, 3, [4, [5, 6]]]]
print deep_reverse(p)
#>>> [[[[6, 5], 4], 3, 2], 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment