Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to understand the details of this code do:

 prune = lambda tree : [prune(branch) for branch in tree if branch != []]

l = [[[[], []], [[], []]], [[], [], []]]
prune(l)


But i have not yet learnt the lambda function.

What I have tried:

I am trying to break it down to the def function format

def prune(tree):
    for branch in tree:
        if branch!=[]:
             prune(branch)
    return branch
print prune([[[[], []], [[], []]], [[], [], []]])


But it doesn't give the same result, what am I doing wrong?
Posted
Updated 6-May-18 6:37am

1 solution

Python
def prune(tree):
    result = []
    for branch in tree:
        if branch != []:
            result.append(prune(branch))
    return result

You were almost there; the only step you missed, was adding the elements to a result list.
 
Share this answer
 
Comments
Maciej Los 6-May-18 16:12pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900