0

I have been searching how to do this for the past while but can't seem to find anything that answers my problem or its idea and code is too complex for me to understand as a complete beginner. So basically this is the task I have to do:

Write a function all sublists(lst) that for a list lst returns as its result a list of all of sublists of lst. A sublist is a list containing a contiguous portion of the original i.e. comprising zero or more consecutive elements from the orginal.

For example, for the list [1, 2, 3] the result should be

[[], [1], [2], [3], [1, 2], [2, 3], [1, 2, 3]]

What I started off doing was creating a whole list containing all of the numbers and then split it. However I can't use the split function since its a string and don't know any proper ways to splice it properly.

  • 1
    This might help you https://stackoverflow.com/questions/1482308/how-to-get-all-subsets-of-a-set-powerset – uguros Dec 17 '21 at 19:18
  • 1
    Hint- try to go over your list and then print all of the sublists that begin with this element. – Lior Pollak Dec 17 '21 at 19:18

2 Answers2

1

use itertools.combinations

from itertools import combinations

l = [1, 2, 3]
final = []
for i in range(len(l)+1):
  final += list(combinations(l,i))

print(final)

[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]

Yefet
  • 2,010
  • 1
  • 10
  • 19
  • note that OP didn't ask for all combinations, OP's expected output is a list of *contiguous sublists* which is a subset of all lengths of combinations. –  Dec 17 '21 at 23:20
0

Here is a function that finds your desired outcome using a double-loop.

def get_contiguous_sublists(lst):
    out = [[]]

    # find the length of the input list (added 1 for convenience which will be useful later)
    len_lst = len(lst) + 1
    
    # for each integer between 1 and the full length of the input list,
    # we slice the input list `lst` to create new lists of this length
    # and add it to the output list `out`
    for length in range(1, len_lst):
        # here, we are changing the starting point of the list slicing, 
        # i.e. whether we want to start from 1 or 2 or 3 for [1,2,3]
        for i in range(len_lst - length):
            out += [lst[i : i + length]]
    return out

Output:

>>> get_contiguous_sublists([1,2,3])
[[], [1], [2], [3], [1, 2], [2, 3], [1, 2, 3]]


>>> get_contiguous_sublists([1,2,3,4])
[[], [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4], [1, 2, 3, 4]]
  • what does the + sign mean – Amal Jaimon Dec 18 '21 at 22:22
  • and does the [[]] mean the same as an empty list [] – Amal Jaimon Dec 18 '21 at 22:22
  • @AmalJaimon because we are creating a list of lists, we initialized it with `[[]]`, since you wanted the empty list as part of the expected outcome; if you don't want the empty list as part of the final outcome, you can just use `[]`. `+` sign is used to add a slice of a list (which itself is a list) to the `out` list (which is a list of lists). –  Dec 18 '21 at 22:27