Why in the step list(subset[I])
is required?
If I do not put list()
I get a wrong answer.
The question is regarding finding subsets from a unique list like [1,2,3]
the answer should be [],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]
The code below is the correct answer -- but I am just confused why the list()
is required in the code. Running without that gives a completely different answer.
def subsets(nums):
subset = []
subset.append([])
for num in nums:
for i in range(len(subset)):
new_list = list(subset[i])
new_list.append(num)
subset.append(new_list)
return subset