Sub lists of list

Multi tool use
Multi tool use












2















I would like to have a function split_list([1,2,4,3]) which yields the following 7 lists:



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


where the smaller lists are yielded first - [[1], [2,4,3]] has length 2 while [[1],[2],[4],[3]] is yielded last because its length is 4. This is as far as I have gotten with it:



def split_list(l):
for i in range(1, len(l)):
yield [l[:i],l[i:]]

for i in split_list((1,2,4,3)):
print(i)









share|improve this question

























  • What do you mean by smaller lists?

    – HakunaMaData
    Dec 28 '18 at 21:37













  • @RobS The length of [[1], [2,4,3]] is 2 while the length of [[1],[2],[4],[3]] is 4.

    – Baz
    Dec 28 '18 at 21:40











  • maybe dupe: How to find all partitions of a list S into k subsets (can be empty)?

    – wim
    Dec 28 '18 at 21:41











  • or: How to split a list into n groups in all possible combinations of group length and elements within group?

    – wim
    Dec 28 '18 at 21:42






  • 1





    Do you want to exclude the main list as the first element? For example [[1, 2, 3, 4]]

    – Daniel Mesejo
    Dec 28 '18 at 22:02
















2















I would like to have a function split_list([1,2,4,3]) which yields the following 7 lists:



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


where the smaller lists are yielded first - [[1], [2,4,3]] has length 2 while [[1],[2],[4],[3]] is yielded last because its length is 4. This is as far as I have gotten with it:



def split_list(l):
for i in range(1, len(l)):
yield [l[:i],l[i:]]

for i in split_list((1,2,4,3)):
print(i)









share|improve this question

























  • What do you mean by smaller lists?

    – HakunaMaData
    Dec 28 '18 at 21:37













  • @RobS The length of [[1], [2,4,3]] is 2 while the length of [[1],[2],[4],[3]] is 4.

    – Baz
    Dec 28 '18 at 21:40











  • maybe dupe: How to find all partitions of a list S into k subsets (can be empty)?

    – wim
    Dec 28 '18 at 21:41











  • or: How to split a list into n groups in all possible combinations of group length and elements within group?

    – wim
    Dec 28 '18 at 21:42






  • 1





    Do you want to exclude the main list as the first element? For example [[1, 2, 3, 4]]

    – Daniel Mesejo
    Dec 28 '18 at 22:02














2












2








2








I would like to have a function split_list([1,2,4,3]) which yields the following 7 lists:



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


where the smaller lists are yielded first - [[1], [2,4,3]] has length 2 while [[1],[2],[4],[3]] is yielded last because its length is 4. This is as far as I have gotten with it:



def split_list(l):
for i in range(1, len(l)):
yield [l[:i],l[i:]]

for i in split_list((1,2,4,3)):
print(i)









share|improve this question
















I would like to have a function split_list([1,2,4,3]) which yields the following 7 lists:



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


where the smaller lists are yielded first - [[1], [2,4,3]] has length 2 while [[1],[2],[4],[3]] is yielded last because its length is 4. This is as far as I have gotten with it:



def split_list(l):
for i in range(1, len(l)):
yield [l[:i],l[i:]]

for i in split_list((1,2,4,3)):
print(i)






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 21:41







Baz

















asked Dec 28 '18 at 21:33









BazBaz

1,5462778191




1,5462778191













  • What do you mean by smaller lists?

    – HakunaMaData
    Dec 28 '18 at 21:37













  • @RobS The length of [[1], [2,4,3]] is 2 while the length of [[1],[2],[4],[3]] is 4.

    – Baz
    Dec 28 '18 at 21:40











  • maybe dupe: How to find all partitions of a list S into k subsets (can be empty)?

    – wim
    Dec 28 '18 at 21:41











  • or: How to split a list into n groups in all possible combinations of group length and elements within group?

    – wim
    Dec 28 '18 at 21:42






  • 1





    Do you want to exclude the main list as the first element? For example [[1, 2, 3, 4]]

    – Daniel Mesejo
    Dec 28 '18 at 22:02



















  • What do you mean by smaller lists?

    – HakunaMaData
    Dec 28 '18 at 21:37













  • @RobS The length of [[1], [2,4,3]] is 2 while the length of [[1],[2],[4],[3]] is 4.

    – Baz
    Dec 28 '18 at 21:40











  • maybe dupe: How to find all partitions of a list S into k subsets (can be empty)?

    – wim
    Dec 28 '18 at 21:41











  • or: How to split a list into n groups in all possible combinations of group length and elements within group?

    – wim
    Dec 28 '18 at 21:42






  • 1





    Do you want to exclude the main list as the first element? For example [[1, 2, 3, 4]]

    – Daniel Mesejo
    Dec 28 '18 at 22:02

















What do you mean by smaller lists?

– HakunaMaData
Dec 28 '18 at 21:37







What do you mean by smaller lists?

– HakunaMaData
Dec 28 '18 at 21:37















@RobS The length of [[1], [2,4,3]] is 2 while the length of [[1],[2],[4],[3]] is 4.

– Baz
Dec 28 '18 at 21:40





@RobS The length of [[1], [2,4,3]] is 2 while the length of [[1],[2],[4],[3]] is 4.

– Baz
Dec 28 '18 at 21:40













maybe dupe: How to find all partitions of a list S into k subsets (can be empty)?

– wim
Dec 28 '18 at 21:41





maybe dupe: How to find all partitions of a list S into k subsets (can be empty)?

– wim
Dec 28 '18 at 21:41













or: How to split a list into n groups in all possible combinations of group length and elements within group?

– wim
Dec 28 '18 at 21:42





or: How to split a list into n groups in all possible combinations of group length and elements within group?

– wim
Dec 28 '18 at 21:42




1




1





Do you want to exclude the main list as the first element? For example [[1, 2, 3, 4]]

– Daniel Mesejo
Dec 28 '18 at 22:02





Do you want to exclude the main list as the first element? For example [[1, 2, 3, 4]]

– Daniel Mesejo
Dec 28 '18 at 22:02












3 Answers
3






active

oldest

votes


















1














If including the list itself as the first element is not a problem, you could so something like this:



def split_list(lst):
if not lst:
yield

for i in range(len(lst), 0, -1):
for j in split_list(lst[i:]):
yield [lst[:i]] + j


for l in split_list([1, 2, 4, 3]):
print(l)


Output



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





share|improve this answer































    1














    You can utilize list slicing with recursion:



    def split_list(d):
    for i in range(len(d)):
    if len(d[i+1:]) > 0:
    for c in split_list(d[i+1:]):
    yield [d[:i+1], *c]
    else:
    yield [d]

    print(list(split_list([1,2,4,3])))


    Output:



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


    Edit: event shorter



    def split_list(d):
    if len(d) > 1:
    return [list(filter(None, [d[:i+1], *c])) for i in range(len(d)) for c in split_list(d[i+1:])]
    return [d] if not isinstance(d, list) else [[d]]


    Output:



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





    share|improve this answer


























    • Nice!! What does the * in *c do? unpack the list?

      – HakunaMaData
      Dec 28 '18 at 22:07











    • @RobS Yes, that is correct. *c is simple unpacking. I think it is a bit cleaner (although that is somewhat subjective) that concatenating lists with +.

      – Ajax1234
      Dec 28 '18 at 22:15



















    1














    Try this one:



    def split_list(l):
    if len(l) == 1:
    yield [ l ]
    return

    for s in split_list(l[1:]):
    for n, sub in enumerate(s):
    if len((s[:n] + [[ l[0] ] + sub] + s[n+1:])[0]) !=4:
    yield s[:n] + [[ l[0] ] + sub] + s[n+1:]
    yield [[l[0]]] + s

    for lst in split_list([1, 2, 3, 4]):
    print lst


    The problem is solved recursively.
    And in output:



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





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53964502%2fsub-lists-of-list%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If including the list itself as the first element is not a problem, you could so something like this:



      def split_list(lst):
      if not lst:
      yield

      for i in range(len(lst), 0, -1):
      for j in split_list(lst[i:]):
      yield [lst[:i]] + j


      for l in split_list([1, 2, 4, 3]):
      print(l)


      Output



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





      share|improve this answer




























        1














        If including the list itself as the first element is not a problem, you could so something like this:



        def split_list(lst):
        if not lst:
        yield

        for i in range(len(lst), 0, -1):
        for j in split_list(lst[i:]):
        yield [lst[:i]] + j


        for l in split_list([1, 2, 4, 3]):
        print(l)


        Output



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





        share|improve this answer


























          1












          1








          1







          If including the list itself as the first element is not a problem, you could so something like this:



          def split_list(lst):
          if not lst:
          yield

          for i in range(len(lst), 0, -1):
          for j in split_list(lst[i:]):
          yield [lst[:i]] + j


          for l in split_list([1, 2, 4, 3]):
          print(l)


          Output



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





          share|improve this answer













          If including the list itself as the first element is not a problem, you could so something like this:



          def split_list(lst):
          if not lst:
          yield

          for i in range(len(lst), 0, -1):
          for j in split_list(lst[i:]):
          yield [lst[:i]] + j


          for l in split_list([1, 2, 4, 3]):
          print(l)


          Output



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 22:04









          Daniel MesejoDaniel Mesejo

          15.8k21030




          15.8k21030

























              1














              You can utilize list slicing with recursion:



              def split_list(d):
              for i in range(len(d)):
              if len(d[i+1:]) > 0:
              for c in split_list(d[i+1:]):
              yield [d[:i+1], *c]
              else:
              yield [d]

              print(list(split_list([1,2,4,3])))


              Output:



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


              Edit: event shorter



              def split_list(d):
              if len(d) > 1:
              return [list(filter(None, [d[:i+1], *c])) for i in range(len(d)) for c in split_list(d[i+1:])]
              return [d] if not isinstance(d, list) else [[d]]


              Output:



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





              share|improve this answer


























              • Nice!! What does the * in *c do? unpack the list?

                – HakunaMaData
                Dec 28 '18 at 22:07











              • @RobS Yes, that is correct. *c is simple unpacking. I think it is a bit cleaner (although that is somewhat subjective) that concatenating lists with +.

                – Ajax1234
                Dec 28 '18 at 22:15
















              1














              You can utilize list slicing with recursion:



              def split_list(d):
              for i in range(len(d)):
              if len(d[i+1:]) > 0:
              for c in split_list(d[i+1:]):
              yield [d[:i+1], *c]
              else:
              yield [d]

              print(list(split_list([1,2,4,3])))


              Output:



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


              Edit: event shorter



              def split_list(d):
              if len(d) > 1:
              return [list(filter(None, [d[:i+1], *c])) for i in range(len(d)) for c in split_list(d[i+1:])]
              return [d] if not isinstance(d, list) else [[d]]


              Output:



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





              share|improve this answer


























              • Nice!! What does the * in *c do? unpack the list?

                – HakunaMaData
                Dec 28 '18 at 22:07











              • @RobS Yes, that is correct. *c is simple unpacking. I think it is a bit cleaner (although that is somewhat subjective) that concatenating lists with +.

                – Ajax1234
                Dec 28 '18 at 22:15














              1












              1








              1







              You can utilize list slicing with recursion:



              def split_list(d):
              for i in range(len(d)):
              if len(d[i+1:]) > 0:
              for c in split_list(d[i+1:]):
              yield [d[:i+1], *c]
              else:
              yield [d]

              print(list(split_list([1,2,4,3])))


              Output:



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


              Edit: event shorter



              def split_list(d):
              if len(d) > 1:
              return [list(filter(None, [d[:i+1], *c])) for i in range(len(d)) for c in split_list(d[i+1:])]
              return [d] if not isinstance(d, list) else [[d]]


              Output:



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





              share|improve this answer















              You can utilize list slicing with recursion:



              def split_list(d):
              for i in range(len(d)):
              if len(d[i+1:]) > 0:
              for c in split_list(d[i+1:]):
              yield [d[:i+1], *c]
              else:
              yield [d]

              print(list(split_list([1,2,4,3])))


              Output:



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


              Edit: event shorter



              def split_list(d):
              if len(d) > 1:
              return [list(filter(None, [d[:i+1], *c])) for i in range(len(d)) for c in split_list(d[i+1:])]
              return [d] if not isinstance(d, list) else [[d]]


              Output:



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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 28 '18 at 22:09

























              answered Dec 28 '18 at 22:02









              Ajax1234Ajax1234

              40.8k42653




              40.8k42653













              • Nice!! What does the * in *c do? unpack the list?

                – HakunaMaData
                Dec 28 '18 at 22:07











              • @RobS Yes, that is correct. *c is simple unpacking. I think it is a bit cleaner (although that is somewhat subjective) that concatenating lists with +.

                – Ajax1234
                Dec 28 '18 at 22:15



















              • Nice!! What does the * in *c do? unpack the list?

                – HakunaMaData
                Dec 28 '18 at 22:07











              • @RobS Yes, that is correct. *c is simple unpacking. I think it is a bit cleaner (although that is somewhat subjective) that concatenating lists with +.

                – Ajax1234
                Dec 28 '18 at 22:15

















              Nice!! What does the * in *c do? unpack the list?

              – HakunaMaData
              Dec 28 '18 at 22:07





              Nice!! What does the * in *c do? unpack the list?

              – HakunaMaData
              Dec 28 '18 at 22:07













              @RobS Yes, that is correct. *c is simple unpacking. I think it is a bit cleaner (although that is somewhat subjective) that concatenating lists with +.

              – Ajax1234
              Dec 28 '18 at 22:15





              @RobS Yes, that is correct. *c is simple unpacking. I think it is a bit cleaner (although that is somewhat subjective) that concatenating lists with +.

              – Ajax1234
              Dec 28 '18 at 22:15











              1














              Try this one:



              def split_list(l):
              if len(l) == 1:
              yield [ l ]
              return

              for s in split_list(l[1:]):
              for n, sub in enumerate(s):
              if len((s[:n] + [[ l[0] ] + sub] + s[n+1:])[0]) !=4:
              yield s[:n] + [[ l[0] ] + sub] + s[n+1:]
              yield [[l[0]]] + s

              for lst in split_list([1, 2, 3, 4]):
              print lst


              The problem is solved recursively.
              And in output:



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





              share|improve this answer




























                1














                Try this one:



                def split_list(l):
                if len(l) == 1:
                yield [ l ]
                return

                for s in split_list(l[1:]):
                for n, sub in enumerate(s):
                if len((s[:n] + [[ l[0] ] + sub] + s[n+1:])[0]) !=4:
                yield s[:n] + [[ l[0] ] + sub] + s[n+1:]
                yield [[l[0]]] + s

                for lst in split_list([1, 2, 3, 4]):
                print lst


                The problem is solved recursively.
                And in output:



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





                share|improve this answer


























                  1












                  1








                  1







                  Try this one:



                  def split_list(l):
                  if len(l) == 1:
                  yield [ l ]
                  return

                  for s in split_list(l[1:]):
                  for n, sub in enumerate(s):
                  if len((s[:n] + [[ l[0] ] + sub] + s[n+1:])[0]) !=4:
                  yield s[:n] + [[ l[0] ] + sub] + s[n+1:]
                  yield [[l[0]]] + s

                  for lst in split_list([1, 2, 3, 4]):
                  print lst


                  The problem is solved recursively.
                  And in output:



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





                  share|improve this answer













                  Try this one:



                  def split_list(l):
                  if len(l) == 1:
                  yield [ l ]
                  return

                  for s in split_list(l[1:]):
                  for n, sub in enumerate(s):
                  if len((s[:n] + [[ l[0] ] + sub] + s[n+1:])[0]) !=4:
                  yield s[:n] + [[ l[0] ] + sub] + s[n+1:]
                  yield [[l[0]]] + s

                  for lst in split_list([1, 2, 3, 4]):
                  print lst


                  The problem is solved recursively.
                  And in output:



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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 28 '18 at 22:55









                  SseinSsein

                  1,0141921




                  1,0141921






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53964502%2fsub-lists-of-list%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      EEo7oiKSJ,zJ4,ucBo4AXnwjbeg7lwsEdDbJha3QLAv3ovEqdVkhOQ9kGA7OATF3xYs1glDwHzTblbf6gb7TcbuIsJT78
                      4xIQBLT,dgOrHDy,TAKU9pIJ5RNh3xerRotFpy6e5wTv62BT,7U4wny1M rL y2xh,jvAZ4QL,RtoCgcYz4yc4

                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas