recursive map on nested list

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Tue Mar 21 08:26:41 EST 2006


I think for most purposes a program like this is short enough:

def recur_map2(fun, data):
    if hasattr(data, "__iter__"):
        return [recur_map2(fun, elem) for elem in data]
    else:
        return fun(data)

data = [set([1, 2]), [3], 4, [5, {6:4}, [7, 8]]]
print recur_map2(lambda x: x*2, data)

Bye,
bearophile




More information about the Python-list mailing list