Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7415921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:23:30+00:00 2026-05-29T07:23:30+00:00

This follows on from this question: Algorithm to generate spanning set Given this input:

  • 0

This follows on from this question:

Algorithm to generate spanning set

Given this input: [1,2,3,4]

I’d like to generate this set of sets in python:

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

So unlike the previous question, the order of the list is retained.

Ideally the code would work for n items in the list

Thanks very much

EDIT 2: Could anyone advise me on how to do this if the original input is a string rather than a list (where each word in the string becomes an item in a list). Thanks!

EDIT: added [1] [2, 3, 4] Sorry for the mistake

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:23:31+00:00Added an answer on May 29, 2026 at 7:23 am

    You might also enjoy a recursive solution:

    def span(lst):
      yield [lst]
      for i in range(1, len(lst)):
        for x in span(lst[i:]):
          yield [lst[:i]] + x
    

    Explanation

    We exploit recursion here to break the problem down. The approach is the following:

    For every list, the whole list is a valid spanning: [1,2,3,4] => [[1,2,3,4]].

    For every list that is longer than size 1, we can use the first item as a group and then apply the same algorithm on the remaining list to get all the combined results:

    [1,2,3] => 
      [[1]] + [[2], [3]]  # => [[1], [2], [3]]
      [[1]] + [[2,3]]     # => [[1], [2,3]]
    

    For every list that is longer than size 2, we can just as well use the first two items as a group and then apply the same algorithm on the remaining list and combine the results:

    [1,2,3,4,5] =>
      [[1,2]] + [[3], [4], [5]]  # => [[1,2], [3], [4], [5]]
      [[1,2]] + [[3,4], [5]]     # => [[1,2], [3,4], [5]]
      [[1,2]] + [[3], [4,5]]     # => [[1,2], [3], [4,5]]
      [[1,2]] + [[3,4,5]]        # => [[1,2], [3,4,5]]
    

    We can see that the possible combinations on the right side are indeed all possible groupings of the remainder of the list, [3,4,5].

    For every list that is longer than … etc. Thus, the final algorithm is the following:

    1. yield the whole list (it is always a valid spanning, see above)
    2. For every possible splitting of the list, yield the left-hand part of the list combined with all possible spannings of the right-hand part of the list.

    yield is a special keyword in Python that make the function a generator, which means that it returns a iterable object that can be used to enumerate all results found. You can transform the result into a list using the list constructor function: list(span([1,2,3,4])).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Follows on from this question Algorithm to generate (not quite) spanning set in Python
This question follows on from a previous question, that has raised a further issue.
This problem follows on from a previous question . When I run the following
OK, this kind of follows on from my previous question . What I would
This follows on from my previous question about Moose structured types. I apologise for
This question follows on from MYSQL join results set wiped results during IN ()
This question follows on from the answer given by Michael Pilat in Preventing Plus
This question follows on from a previous question... How do I create the current
This question follows on from a previous question which can be found here: Need
This question follows on from a previous question. However stackoverflow presents me from commenting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.