Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when attempting to use the 'pop' method with a list in Python [closed]

Tags:

python

list

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?

like image 526
c3ntury Avatar asked Mar 07 '26 21:03

c3ntury


1 Answers

.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)
like image 183
eumiro Avatar answered Mar 10 '26 11:03

eumiro