Ask Your Question

Revision history [back]

Yes, you are right: map no longer returns a list but rather an iterator. So you can do e.g.:

def tree(expr):     
    if expr.operator() is None: 
        return expr 
    else: 
        return [expr.operator()]+list(map(tree, expr.operands()))

Yes, you are right: map no longer returns a list but rather an iterator. So you can do e.g.:

def tree(expr):     
    if expr.operator() is None: 
        return expr 
    else: 
        return [expr.operator()]+list(map(tree, expr.operands()))

You can also avoid map and write a list comprehension instead:

def tree(expr):     
    if expr.operator() is None: 
        return expr 
    else: 
        return [expr.operator()]+[tree(op) for op in expr.operands()]