I was wondering if there was a relatively simple way to find all subsets of list iteratively. Using recursion, this is easily done and very concise...
def partial(alist, pos, chosen):
if pos == len(alist):
return [chosen]
else:
return partial(a list, pos+1, chosen) \
+ partial(alist, pos+1, chosen + [alist[pos]])
This will return a list of lists containing all subsets of a list.
Is there a way to do something like this iteratively without it being overly complicated, or is recursion the best way? Some pseudo-code or something explaining a way to do this iteratively would be helpful. I know itertools
is helpful but I would like a solution without the need for it if possible.