I want to take a nested list
[[[1,2],[4,5]], 1, [1,3,4],[2,3,4]]
go in there recursively, modify each value, and then return a similar list. For example say i wanted to add 1 to each value the output would be
[[[2,3],[5,6]], 2, [2,4,5],[3,4,5]]
I've made some progress doing this with making a recursive function where i test if it's a list or a element to modify. I'm stumped when it comes to how to compile the list again. Isn't there a simple way to do this? The following code is what i have so far.
def add1(nodelist):
list_of_lists = []
name_nodes.nodes = []
def recurse_list(nodelist):
name_nodes.nodes = []
edit_list = False
for r in nodelist:
if type(r) == list:
recurse_list(r)
else:
edit_list = True
name_nodes.nodes.append(r+1)
if edit_list == True:
list_of_lists.append(name_nodes.nodes)
recurse_list(nodelist)
return list_of_lists
This code gets me the following output
[[2, 3], [5, 6, 2], [2, 4, 5], [3, 4, 5], [3, 4, 5]]
I'm surprised there's not some module or some built in functionality to better handle nested lists because there are so many questions handling similar but different behavior. The closest question i could find is here. But that just didn't have exactly what i was looking for. Thanks for any answers.