Created
February 8, 2013 01:33
-
-
Save hackjoy/4735883 to your computer and use it in GitHub Desktop.
Reverse every element within a list including nested lists.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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