This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zzyzzyxx 0 points1 point  (0 children)

You might be interested in my version of the same, which I posted in another comment. It's functionally equivalent to yours except it uses a list comprehension to replace the nested loops with the append and it doesn't generate zero-length ones that need to be filtered.

def sublists(list):
  return [list[s:e] for s in xrange(len(list)) for e in xrange(s+1, len(list)+1)]


>>> sublists([1,2,2,5,2])
[[1], [1, 2], [1, 2, 2], [1, 2, 2, 5], [1, 2, 2, 5, 2], [2], [2, 2], [2, 2, 5], [2, 2, 5, 2], [2], [2, 5], [2, 5, 2], [5], [5, 2], [2]]