# Please explain the working of this code? [closed]

Break it down into pieces

``````def gSubsets(L): #recursive function
if len(L) == 0: #when weve reached the last subset we then have to handle an empty list
print '2'
return [[]] #returns a list of lists for the small in smaller
smaller = gSubsets(L[:-1]) #get subsets by recursive call for all elements in the list except the last one
extra = L[-1:] #get the last element in the list not used in this recursive call
print L
new = []
for small in smaller: #loop through list of lists from recursive call
new.append(small+extra) #append all combinations of the last element in the list to every other element in the same list to new
return smaller+new #return subset with new combinations

print gSubsets([1,2])
``````

this outputs

``````>>> 2
>>> [1]
>>> [1, 2]
>>> [[], [1], [2], [1, 2]]
``````

by the way, in python you should use underscores in your variable and function names (its the preferred syntax) and I would work on your variable names too.. you want them to be very specific so anyone else coming by can understand what it is right away.. this is how I would rename the variables.

``````def generate_subsets_from_list(input_list):
if len(input_list) == 0:
# print '2'  -- not sure why you are printing 2 here?
return [[]]
subsets = generate_subsets_from_list(input_list[:-1])
last_element = L[-1:]
print L
return_list = []
for subset in subsets:
return_list.append(subset+last_element)
return subsets+return_list

initial_list = [1,2]
print generate_subsets_from_list(initial_list)
``````