"res = [[]]" creates a variable local to driver(). "res = res + temp" creates a variable local to subsets(). Not only are these different variables, but they reference different lists. The list returned by driver is not the list being modified in subsets(). This is shown if you print out the object ID for res.
def driver(arr):
res = [[]]
def subsets(arr, res):
print("res is a variable passed into subsets as", id(res))
if arr == []:
return
subsets(arr[1:],res)
temp = res
#here temp = [[]] for the inner most function call
for subset in res:
subset.append(arr[0])
res = res + temp
print("assignment of res inside subsets creates a local array", id(res))
print("res starts out as", id(res))
subsets(arr, res)
return res
res = driver([1])
print("The res returned by driver is the res defined in driver, not subsets", id(res))
Output:
res starts out as 2255377308416
res is a variable passed into subsets as 2255377308416
res is a variable passed into subsets as 2255377308416
assignment of res inside subsets creates a local array 2255377308352
The res returned by driver is the res defined in driver, not subsets 2255377308416
temp = res does not make temp a copy of res, not even a shallow copy. Assignment creates a new variable, but the variable references the same list as res. "res = res + temp" is exactly the same as "res = res + res". Not only do you want to copy, I think you want a deep copy.
Is this what you are looking for?
import copy
def driver(arr):
res = [[]]
def subsets(arr, res):
if arr == []:
return
subsets(arr[1:], res)
temp = copy.deepcopy(res)
#here temp = [[]] for the inner most function call
for subset in res:
subset.append(arr[0])
res[:] = res + temp
subsets(arr, res)
return res
print(driver([1, 2, 3]))
Output:
[[3, 2, 1], [2, 1], [3, 1], [1], [3, 2], [2], [3], []]
If I understood what you wanted to accomplish I might know a better way to do it. For example, this code achieves the same thing (almost).
def subsets(values):
results = [[]]
for value in values:
for subset in results[:]:
results.append([value] + subset)
return results
print(subsets([1, 2, 3]))
Output:
[[], [1], [2], [2, 1], [3], [3, 1], [3, 2], [3, 2, 1]]
And if a list of combinations is all you want there are functions in the itertools library that will do this shorter and faster.