A
alexandre_irrthum
Hello,
I'd like to apply a function to elements of a nested list and wondered
if there is anything more idiomatic and/or shorter than this recursive
way:
.... if isinstance(data, list):
.... mapped_list = []
.... for i in data:
.... mapped_list.append(recur_map(f, i))
.... return mapped_list
.... else:
.... return f(data)
....
Thanks,
alex
I'd like to apply a function to elements of a nested list and wondered
if there is anything more idiomatic and/or shorter than this recursive
way:
.... if isinstance(data, list):
.... mapped_list = []
.... for i in data:
.... mapped_list.append(recur_map(f, i))
.... return mapped_list
.... else:
.... return f(data)
....
[[2, 4], 6, 8, [10, 12, [14, 16]]]recur_map(lambda x: x*2, [[1, 2], 3, 4, [5, 6, [7, 8]]])
Thanks,
alex