I wanted to build a function that finds all the possible subsets of a set. The first function works well, while the second doesn't. In the first I used a list comprehension to remove the first element of a list, while in the second I used pop(0). Why do they output different results? The first function is:
def combinations(n):
if len(n) == 1:
return [[], n]
a = [element for element in n if element != n[0]]
return [[n[0]] + comb for comb in combinations(a)] + combinations(a)
The second function is:
def combinations(n):
if len(n) == 1:
return [[], n]
a = n
a.pop(0)
return [[n[0]] + comb for comb in combinations(a)] + combinations(a)
The output of the first is:
[[0, 1, 2], [0, 1, 2, 3], [0, 1], [0, 1, 3], [0, 2], [0, 2, 3], [0], [0, 3], [1, 2], [1, 2, 3], [1], [1, 3], [2], [2, 3], [], [3]]
The output of the second is:
[[3, 3, 3], [3, 3, 3, 3], [3, 3], [3, 3, 3], [3], [3, 3], [], [3]]