I'm trying to code a simple procedure, that simply reserves an input list (including nested lists).
The procedure itself -
def is_list(p):
return isinstance(p, list)
def deep_reverse(input):
result = []
if len(input) == 0:
return []
for element in input:
if is_list(element) == True:
deep_reverse(element)
else:
appended = input.pop
result.append(appended)
print result
return result
What I'm inputting to execute the procedure -
p = [1, [2, 3, [4, [5, 6]]]]
print deep_reverse(p)
print p
Result -
[built-in method pop of list object at 0xb752b0cc]
[built-in method pop of list object at 0xb7529c6c]
[built-in method pop of list object at 0xb7529cec]
[built-in method pop of list object at 0xb7529ecc]
[built-in method pop of list object at 0xb7529ecc]
[1, [2, 3, [4, [5, 6]]]]
What am I doing wrong?
.pop is a method. You have to call it:
appended = input.pop()
This will take the last element from input. If you want to take the first element, give it an argument:
appended = input.pop(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With