n = len(graph)-1
def dfs(graph, i, part, res):
part.append(i)
if i==n:
res.append(part)
return
for j in graph[i]:
index = len(part)
dfs(graph, j, list(part), res)
# part = part[:index]
if __name__ == "__main__":
res = []
part = []
graph = [[1,2],[3],[3],[]]
Earlier I was using part = part[:index]
, as in python list is always passed as reference and thus value gets updated. But, then I came across list(part)
, it actually removes the appended values in last recursive call, and works as expected.
Can someone explain what actually happens here?