Skip to content

Instantly share code, notes, and snippets.

@ramaseshan
Created May 19, 2015 10:49
Show Gist options
  • Save ramaseshan/3b97a6ed7c05b8da64af to your computer and use it in GitHub Desktop.
Save ramaseshan/3b97a6ed7c05b8da64af to your computer and use it in GitHub Desktop.
Generate all the possible sublists of a list
def sublists(symps):
# sample input [1,2,3] Ex Output [[1],[2],[3],[1,2],[2,3],[1,3],[1,2,3]]
length = len(symps)
i = 0
poss = []
while i < length:
temp = []
size = length - i
temp = [sublist for sublist in (symps[x:x+size] for x in range(len(symps) - size + 1))]
i = i +1
#for l in temp:
#poss.append(temp)
for l in temp:
poss.append(l)
return poss
print sublists([1,2,3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment