Shortly after I submitted my question and left for a while to grab the byte, I thought of one answer that is different from all the others published so far ... and after a bit of optimization, apart from some initialization, is relatively simple.
One of the function’s arguments is count , which allows you to partially initialize the resulting list in advance, eliminating the need to grow it with a few append calls:
def multimap(value, f, count): result = [] if count < 1 else [f[value]] + [None]*(count-1) for i in range(1, count): result[i] = f[result[i-1]] return result
source share