N,M=map(int,input().split())
ans=[]
lst=[]
def dfs(num):
global ans
if(num==M):
ans.append(lst)
return
for i in range(1,N+1):
if(i not in lst):
lst.append(i)
dfs(len(lst))
print(ans)
lst.pop()
print(ans)
dfs(0)
print(ans)
this is my code. This is example code, when I put N=4, M=1. execution result
I don't understand why my list called ans is reset, when after 'lst.pop()' Two lists are actually seperated lists. Thank you