what i want is to change all elements in a list with an unknown initial shape, so i made this helper function, but is there a better (shorter) way, or a builtin to do this?
and also another question on the side, where is the best place to store such a function >if< there is no builtin, instead of copy paste it in every file, or project
def getNextList():
yield [1]
yield [[1,2],3]
yield [[1,2,[3]], [4,5],6]
def change(next_list, function):
for i, e in enumerate(next_list):
if(isinstance(e, int)):
next_list[i] = function(next_list[i])
else: change(e, function)
for next_list in getNextList():
change(next_list, lambda x: x+1)
print(next_list)
desired output:
[2]
[[2, 3], 4]
[[2, 3, [4]], [5, 6], 7]
Edit: @Colin Schoen is is not a duplicate of your tagged question, because iterating twice would just give me an Error
i guess the solution i posted, or the version of @blhsing which is indeed shorter, is the only one for that particular problem, but if i stick to it, what would be the way to have that function available across different projects without duplicating it?